AVPlayerItem replaceCurrentItemWithPlayerItem阻塞 [英] AVPlayerItem replaceCurrentItemWithPlayerItem Blocking

查看:299
本文介绍了AVPlayerItem replaceCurrentItemWithPlayerItem阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先关于应用程序的一些背景...

- 有很多涉及视频播放器的大量UI操作(主要是滚动)

- 视频是动态的并且基于变化在我们当前页面上。

- 因此视频必须是动态的并且不断变化,并且UI需要响应

First a little context about the application...
- There's a lot of heavy UI operation's involving video players (mostly scrolling)
- The videos are dynamic and change based on our current page .
- so the video's have to be dynamic and keep changing and also the UI needs to be responsive

我最初使用一个 MPMoviePlayerController 但是由于某些要求我必须回到AVPlayer上?
我为 AVPlayer创建了自己的包装器

要更改videoPlayer中的内容,这是AVPlayer-wrapper Class中的方法。

I was initially using a MPMoviePlayerController but then due to certain requirements i had to fall back on the AVPlayer
I made my own wrapper for the AVPlayer .
To change the content in the videoPlayer this is what the method looks like in the AVPlayer-wrapper Class

/**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];
}

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

Now ofcourse everything was working fine ......except ..

[self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem];

似乎在几分之一秒内阻止了用户界面,在滚动期间这些正在使用户界面真的很棒没有反应和丑陋也不能在后台执行此操作

Seems to be blocking the UI for a fraction of a second and during scrolling these is making the UI really unresponsive and ugly also this operation cannot be performed in the background

这是否有任何修复或解决方法..?

Is there any fix or workaround for this .. ?

推荐答案

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

The solution I found was to ensure that the underlying AVAsset is ready to return basic info, such as its duration, before feeding it to the AVPlayer. AVAsset has a method loadValuesAsynchronouslyForKeys: which is handy for this:

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

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

In my case the URL is a network resource, and replaceCurrentItemWithPlayerItem: will actually block for several seconds waiting for this information to download otherwise.

这篇关于AVPlayerItem replaceCurrentItemWithPlayerItem阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆