【问题标题】:AVPlayerItem replaceCurrentItemWithPlayerItem BlockingAVPlayerItem replaceCurrentItemWithPlayerItem 阻塞
【发布时间】:2013-01-12 19:40:44
【问题描述】:

首先是关于应用程序的一些背景...
- 有很多繁重的 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 非常无响应且丑陋,而且此操作无法在后台执行

是否有任何解决方法或解决方法?

【问题讨论】:

  • 你找到解决办法了吗?
  • @AdrianDemetrescu - 不......必须恢复到 mpmovieplayer
  • 您尝试过这些建议了吗? stackoverflow.com/questions/7420176/…
  • 我没有,如果它确实有效......你可以在这里发布修复
  • 我也被这个困住了......有人吗?

标签: iphone ios ipad mpmovieplayercontroller avplayer


【解决方案1】:

我找到的解决方案是确保底层AVAsset 准备好返回基本信息,例如其持续时间,然后再将其提供给AVPlayerAVAsset 有一个方法 loadValuesAsynchronouslyForKeys: 很方便:

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

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

【讨论】:

  • 根据 Apple Docs,AVPlayerAVPlayerItem 的这些方法需要在主线程上调用。
  • 即使在获得信息('duration')之后,在慢速网络中开始流式传输之前,它也会阻塞 ui 几秒钟
  • 每个人都在谈论必须重新实现自己的 AVPlayer 以便在 UICollectionView 中使用 AVPlayer 进行平滑滚动,但这是解决方案 - 只需在分配之前使用 loadValuesAsynchronously 预加载资产!
  • 我认为观察“可播放”键比“持续时间”更有意义。
【解决方案2】:

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

遗憾的是,这涉及到一种带有某种不一致异步 KVO 的巫毒舞蹈,这并不好玩。

【讨论】:

    猜你喜欢
    • 2012-02-20
    • 2016-07-06
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2016-07-04
    • 1970-01-01
    相关资源
    最近更新 更多