我正在开发一款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中时...
在feed中存在超过18个视频时,新的AVPlayer
实例将无法播放,并且_videoPlayer.currentItem.status
将是带有模糊错误代码Code=-11800 "无法完成操作"
的AVPlayerItemStatusFailed
。有趣的是,无论前18个视频的长度和质量如何,总是第19个视频最先出问题。
我想这些AVPlayer实例不应该全部由我创建。我尝试使用AVPlayer单例包装器,在用户想要播放视频时,我向相关的UIView传递了一个共享的AVPlayer实例。这消除了AVPlayerItemStatusFailed
错误,但使回放效果极其卡顿。
我还没有找到任何关于这个问题的其他解决方案。如果有人能提供一些关于更好的处理此情况的见解,或者指点我一个好的AVPlayer教程,我会非常感激。谢谢!