AVPlayerItem replaceCurrentItemWithPlayerItem阻塞

首先介绍一下应用程序的一些背景……
– 有很多沉重的UI操作涉及video播放器(主要是滚动)
– video是动态的,并根据我们当前的页面进行更改。
– 因此video必须是动态的并且不断变化,并且UI需要响应

我最初使用的是MPMoviePlayerController但由于某些要求,我不得不重新使用AVPlayer
我为AVPlayer制作了自己的包装器。
要更改videoPlayer中的内容,这是AVPlayer-wrapper类中的方法

 /**We need to change the whole playerItem each time we wish to change a video url */ -(void)initializePlayerWithUrl:(NSURL *)url { AVPlayerItem *tempItem = [AVPlayerItem playerItemWithURL:url]; [tempItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil]; [tempItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil]; //Not sure if this should be stopped or paused under the ideal circumstances //These will be changed to custom enums later [self setPlaybackState:MPMoviePlaybackStateStopped]; [self setLoadState:MPMovieLoadStateUnknown]; [self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem]; //This is required only if we wish to pause the video immediately as we change the url //[self.videoPlayer pause]; } 

现在一切都很好……除了……

 [self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem]; 

似乎是阻止UI几分之一秒,在滚动期间这些使得UI真的没有响应和丑陋,这个操作也无法在后台执行

有没有任何修复或解决方法…?

我找到的解决方案是确保底层AVAsset准备好在将其提供给AVPlayer之前返回基本信息,例如其持续时间。 AVAsset有一个方法loadValuesAsynchronouslyForKeys:这对于这个很方便:

 AVAsset *asset = [AVAsset assetWithURL:self.mediaURL]; [asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{ AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:asset]; [self.avPlayer replaceCurrentItemWithPlayerItem:newItem]; }]; 

在我的例子中,URL是一个网络资源,并且replaceCurrentItemWithPlayerItem:实际上将阻塞几秒钟,否则等待此信息下载。

构建Ultravisual时遇到了同样的问题。 我不能完全记住我们是如何解决它的,但是IIRC它涉及在后台线程上尽可能多地完成项目设置,并等到新项目报告它在调用replaceCurrentItemWithPlayerItem之前“准备好播放”。

可悲的是,这涉及一种伏都教舞蹈,异步KVO有些不一致,这并不好玩。