【发布时间】:2015-11-30 23:27:15
【问题描述】:
我正在开发一个 iOS 应用程序,该应用程序创建用户池提交的所有视频的供稿,使用户能够浏览和查看其他人创建的视频。正如您所想象的,我需要能够在提要中支持任意数量的视频。
目前,我正在为每个视频创建并保留一个 AVPlayer 实例,如下所示:
//inside the init method of a UIView
//create the AVPlayer and store it in a strong property
NSString * urlString = @"aRemoteURL.mov";
NSURL * movURL = [NSURL URLWithString:urlString];
_videoPlayer = [[AVPlayer alloc]initWithURL:movURL];
//register callbacks for handling buffering
[_videoPlayer.currentItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[_videoPlayer.currentItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
//add the AVPlayerLayer to the view so you can see the video
AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:_videoPlayer];
playerLayer.frame = self.frame;
[self.layer addSublayer:playerLayer];
当用户点击UIView 时,我在_videoPlayer 上调用play,一切正常。也就是说,直到有足够多的视频被提交到 Feed...
一旦提要中存在超过 18 个视频,AVPlayer 的新实例将无法播放,_videoPlayer.currentItem.status 将变为 AVPlayerItemStatusFailed,并出现模棱两可的错误 Code=-11800 "The operation could not be completed"。有趣的是,无论前 18 个视频的长度和质量如何,总是第 19 个视频最先被破坏。
我认为我不应该创建所有这些 AVPlayer 实例。我尝试使用 AVPlayer 单例包装器,当用户想要播放视频时,我将共享的 AVPlayer 实例传递给相关的 UIView。这消除了AVPlayerItemStatusFailed 错误,但使播放变得无法使用。
我无法找到任何其他有关此问题的帐户。如果有人能提供一些关于这种情况的更好方法的见解,或者甚至可以为我指出一个好的 AVPlayer 教程的方向,我将非常感激。谢谢!
【问题讨论】:
标签: ios objective-c avplayer avplayerlayer avplayeritem