首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayerItem replaceCurrentItemWithPlayerItem阻塞

AVPlayerItem replaceCurrentItemWithPlayerItem阻塞
EN

Stack Overflow用户
提问于 2013-01-29 17:23:35
回答 2查看 8.1K关注 0票数 30

首先简单介绍一下应用程序的上下文...

  • 有很多繁重的UI操作,包括视频播放器(主要是滚动)
  • 视频是动态的,并且会根据当前页面而变化。
  • 所以视频必须是动态的并不断变化,并且UI需要具有响应性

我最初使用的是MPMoviePlayerController,但后来由于某些要求,我不得不求助于AVPlayer

我为AVPlayer做了自己的包装器。

要更改videoPlayer中的内容,方法在AVPlayer-wrapper类中如下所示

代码语言:javascript
复制
/**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 ..

代码语言:javascript
复制
[self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem];

似乎阻塞了UI几分之一秒,在滚动过程中,这些使UI变得非常迟钝和丑陋,而且此操作无法在后台执行

是否有任何修复或解决此问题的方法。?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-23 07:25:58

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

代码语言:javascript
复制
AVAsset *asset = [AVAsset assetWithURL:self.mediaURL];
[asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
    AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:asset];
    [self.avPlayer replaceCurrentItemWithPlayerItem:newItem];
}];

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

票数 23
EN

Stack Overflow用户

发布于 2015-05-21 21:57:14

我们在构建Ultravisual时也遇到了同样的问题。我不太记得我们是如何解决这个问题的,但是IIRC它涉及到在后台线程上做尽可能多的物品设置,并在调用replaceCurrentItemWithPlayerItem之前等待新的物品报告它“可以玩了”。

可悲的是,这涉及到与异步KVO有点不一致的伏都教舞蹈,这并不有趣。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14579458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档