4

Is it possible to pause and resume buffering of AVPlayer?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CharlesFly
  • 121
  • 2
  • 10

3 Answers3

6

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;
    }
abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
  • 3
    Doesn't work for remote media file using progressive downloading. Maybe this is just for live streaming? – coolioxlr Aug 30 '18 at 09:00
1

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.

rpstw
  • 1,582
  • 14
  • 16
0

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

Community
  • 1
  • 1