是否可以暂停和恢复 AVPlayer 的缓冲?

Is it possible to pause and resume buffering of AVPlayer?

是否可以暂停和恢复 AVPlayer 的缓冲?

AV 播放器在多种情况下缓冲视频,但没有关于它们的明确文档。您可以查看 currentItem.loadedTimeRanges 以查看发生了什么 更多信息可以在此线程 AVPlayer buffering, pausing notification, and poster frame

上找到

是的,在某种程度上是可能的!

您可以使用 playerItem 的 preferredForwardBufferDuration 属性 来决定玩家应该预取当前播放时间的持续时间。但遗憾的是,这仅适用于 iOS 版本 10。

检查系统版本的宏:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

您现在可以设置要预取的持续时间(以秒为单位)。

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
    NSTimeInterval interval = 1; // set to  0 for default duration.
    _player.currentItem.preferredForwardBufferDuration = interval;
    _player.automaticallyWaitsToMinimizeStalling = YES;

}

automaticallyWaitsToMinimizeStalling 是另一个 属性,它在 avplayer 中启用 autoplayautowait 功能。因为如果 preferredForwardBufferDuration 设置为较小的持续时间,播放器可能会经常卡顿。

您还可以使用播放器的canUseNetworkResourcesForLiveStreamingWhilePaused 属性来设置当播放器处于暂停状态时播放器是继续还是暂停缓冲。从 iOS 9 开始可用。

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
_player.currentItem.canUseNetworkResourcesForLiveStreamingWhilePaused = NO;
}

The condition check for the system version is very important. You can use the macro mentioned above to do this. Otherwise the app will crash.

更新 - swift:

    if #available(iOS 10.0, *) {
            player.currentItem?.preferredForwardBufferDuration = TimeInterval(1)
            player.automaticallyWaitsToMinimizeStalling = true;
    }

根据我的测试,preferredForwardBufferDuration应该大于或等于1.0,如果低于1.0,将使用默认缓冲时间。