这是我的代码

@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分配给类成员变量,以阻止这种情况的发生。

否则,视频文件可能有问题。

10-08 14:51