ios - AVPlayer 在 iOS7 中不显示视频,在 iOS8 和 iOS9 上工作正常

标签 ios objective-c ios7 avplayer

这是我的代码,

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

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

关于ios - AVPlayer 在 iOS7 中不显示视频,在 iOS8 和 iOS9 上工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360752/

相关文章:

iOS Storyboard 更改初始 View

ios - 无法使用 Swift 为 UILabel 设置动画

ios - 我们什么时候使用saveAccount :(ACAccount *)account of ACAccountStore class?

iphone - 如何将可变数组的对象以数组的形式存储到另一个可变数组中。?

ios - MFMessageComposeViewController iOS7 添加附件数据 :typeIdentifier:filename: not working

ios - 普通 TableView 分隔符未删除

objective-c - 可达性阻止调用 dealloc

objective-c - imageRectForBounds 未在 NSButtonCell 子类中调用

ios - 为什么不将值添加到 NSMutableArray?

ios - 创建地理围栏时 NSCFNumber getCString maxLength 编码错误