Is it possible to pause and resume buffering of AV

2020-05-24 16:26发布

Is it possible to pause and resume buffering of AVPlayer?

标签: ios avplayer
3条回答
Deceive 欺骗
2楼-- · 2020-05-24 17:11

AV player buffers the video in several cases, however no clear documentation on them. You can check currentItem.loadedTimeRanges to see what's going on more info can be found on this thread AVPlayer buffering, pausing notification, and poster frame

查看更多
家丑人穷心不美
3楼-- · 2020-05-24 17:15

Yes it is possible to some extent!

You can use the playerItem's preferredForwardBufferDuration property to decide how much duration from the current playing time the player should prefetch. But sadly, this is only available from iOS version 10.

Macro to check system version:

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

You can now set how much duration you want to prefetch (in seconds).

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 is another property which enables the autoplay or autowait feature in avplayer. Because the player is likely to stall frequently if the preferredForwardBufferDuration is set to a smaller duration.

You can also use the playeritem's canUseNetworkResourcesForLiveStreamingWhilePaused property to set if the player should continue or pause buffering when the player is in pause state. This is available from iOS 9 onwards.

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.

UPDATE - swift:

    if #available(iOS 10.0, *) {
            player.currentItem?.preferredForwardBufferDuration = TimeInterval(1)
            player.automaticallyWaitsToMinimizeStalling = true;
    }
查看更多
Deceive 欺骗
4楼-- · 2020-05-24 17:18

according to my test,preferredForwardBufferDuration should be greater or equal to 1.0,if it is below 1.0,default buffer duration will be used.

查看更多
登录 后发表回答