AVPlayerItem buffer duration not respected

I am trying to set preferredForwardBufferDuration property on AVPlayerItem and haven't been able to get it to work. No matter what non zero value I set, AVPlayer proceeds to buffer the entire file (mp3)


Does this mean, preferredForwardBufferDuration only works for HLS and not progressive download files? If so, is there any other way to control buffer on AVPlayer level?

Replies

Did you ever work this out? I’m making a tvOS app, and have the opposite problem.


My app needs to play a HLS stream very reliably (ie. for presentations in front of a hundred people), so I want to buffer as much as possible to cope with intermittent internet dropouts. However even when I set the preferredForwardBufferDuration to an hour, only ~2-3 minutes is buffered.

Any chance that you found a solution?

Hate to do a +1 but I am also interested in any commentart around this issue.


Cheers! 🙂

Hi, struggeled with this issue myself. I have now found out that you need to set the AVPlayers property automaticallyWaitsToMinimizeStalling to false, in order to get preferredForwardBufferDuration to work.

I'm so sad that automaticallyWaitsToMinimizeStalling doesn't help me.

Think I tried everything here too.. None worked. Entire videos endedup getting buffered right after the following code snippet. Goal here is to quickly keep a lot of short video clips with only the beginnings pre-buffered, so that when users clicks into one of them they will play immediately, and only then have AVPlayer focus on trying to download the rest of that clip. Buffering the entire video clip for all the videos is not only a waste of users' bandwidth, but also given most network condition ends up that 90% of the time the video that the user ends up clicking on is not buffered at all... Playground Project test code I used:


  @IBAction func startPrebuffer(_ sender: UIButton) {
    for videoURLString in videoURLStringArray {
      let avPlayerItem = AVPlayerItem(url: URL(string: videoURLString)!)
      avPlayerItem.preferredForwardBufferDuration = TimeInterval(2.0)
      let avPlayer = AVPlayer(playerItem: avPlayerItem)
      avPlayer.automaticallyWaitsToMinimizeStalling = false
      avPlayerArray.append(avPlayer)
    }


Googling preferredForwardBufferDuration also only gives just a handful of result, mostly not related to this problem. Apple's documentation on this is also very disappointing....


This is Xcode 9.0. Base SDK 11.0, Target Deployment 10.0


Anybody filed a Radar on this by any chance already?

There is an additional note in the AVPlayerItem.h that might explain this:

> Note that the system may buffer less than the value of this property in order to manage resource consumption.

I'm using Xamarin.iOS with Xcode 12 and it works for me fine:

1) set automaticallyWaitsToMinimizeStalling = false
2) set preferredForwardBufferDuration = 10 (seconds)
3) play()

The player is really buffering till 9.7 - 9.9 seconds, that's great! What's not great is that playback does not start automatically. I needed to add an if myself:

4) calculate the total buffered time from player.currentItem.loadedTimeRages
5) if the total buffered time > 9 seconds the run the player by playImmediatelyAtRate(1)
  • Can you explain a little bit more details on how you calculated the total buffered time?

Add a Comment

It looks like it is not possible to set preferredForwardBufferDuration to more than 100.

Facing the same issue. 'automaticallyWaitsToMinimizeStalling' also did not help