[英]AVPlayer not showing video in iOS7, working fine on iOS8 and iOS9
這是我的代碼
@interface ViewController ()
{
AVPlayer *avPlayer;
}
@end
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIView *containerView = [[UIView alloc] initWithFrame:self.view.frame];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"TestVideo" ofType:@"mp4"];
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filepath]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = self.view.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self.view addSubview:containerView];
[avPlayer play];
}
這真的不應該在任何 iOS 版本上工作
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
超出范圍並在viewDidAppear
結束時被釋放。
您應該將AVPlayer
分配給類成員變量,以阻止這種情況發生。
否則視頻文件可能有問題。
您的 AVPlayer 項目需要一個強引用,因為它會自動設置為 nil
請更換
@interface ViewController ()
{
AVPlayer *avPlayer;
}
@end
對此
@interface ViewController ()
@property (strong, nonatomic) AVPlayer *avPlayer;
@end
並將所有avPlayer
引用更改為self.avPlayer
我希望這會avPlayer
幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.