这是我的代码
@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
给类成员变量,以阻止这种情况的发生。
否则,视频文件可能有问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句