Is it possible to pause and resume buffering of AV

2020-05-24 16:35发布

问题:

Is it possible to pause and resume buffering of AVPlayer?

回答1:

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;
    }


回答2:

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.



回答3:

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



标签: ios avplayer