首先简单介绍一下应用程序的上下文...
我最初使用的是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];
}当然,现在一切都运行得很好,......except ..
[self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem];似乎阻塞了UI几分之一秒,在滚动过程中,这些使UI变得非常迟钝和丑陋,而且此操作无法在后台执行
是否有任何修复或解决此问题的方法。?
发布于 2015-09-23 07:25:58
我找到的解决方案是确保在将其提供给AVPlayer之前,底层AVAsset已经准备好返回基本信息,比如它的持续时间。AVAsset有一个loadValuesAsynchronouslyForKeys:方法,可以很方便地做到这一点:
AVAsset *asset = [AVAsset assetWithURL:self.mediaURL];
[asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:asset];
[self.avPlayer replaceCurrentItemWithPlayerItem:newItem];
}];在我的例子中,URL是一个网络资源,否则replaceCurrentItemWithPlayerItem:实际上会阻塞几秒钟,等待下载此信息。
发布于 2015-05-21 21:57:14
我们在构建Ultravisual时也遇到了同样的问题。我不太记得我们是如何解决这个问题的,但是IIRC它涉及到在后台线程上做尽可能多的物品设置,并在调用replaceCurrentItemWithPlayerItem之前等待新的物品报告它“可以玩了”。
可悲的是,这涉及到与异步KVO有点不一致的伏都教舞蹈,这并不有趣。
https://stackoverflow.com/questions/14579458
复制相似问题