AVPlayerItem replaceCurrentItemWithPlayerItem阻塞

Aat*_*asi 28 iphone mpmovieplayercontroller ipad ios avplayer

首先是关于应用程序的一些背景信息......
- 有很多涉及视频播放器的大量UI操作(主要是滚动)
- 视频是动态的,并根据我们当前的页面进行更改.
- 因此视频必须是动态的并且不断变化,并且UI需要响应

我最初使用a 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];
}
Run Code Online (Sandbox Code Playgroud)

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

[self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem];
Run Code Online (Sandbox Code Playgroud)

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

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

Gle*_*idt 20

我发现的解决方案是确保AVAsset基础信息准备好返回基本信息,例如其持续时间,然后再将其提供给AVPlayer.AVAsset有一个loadValuesAsynchronouslyForKeys:方便的方法:

AVAsset *asset = [AVAsset assetWithURL:self.mediaURL];
[asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
    AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:asset];
    [self.avPlayer replaceCurrentItemWithPlayerItem:newItem];
}];
Run Code Online (Sandbox Code Playgroud)

在我的情况下,URL是一个网络资源,replaceCurrentItemWithPlayerItem:实际上会阻塞几秒钟等待此信息下载.