justdan0227
justdan0227

Reputation: 1362

iOS AVPlayer cancel buffering

If I load an AVPlayer with a file from a host with

[AVPlayer playerWithPlayerItem:playerItem];

And the "buffering" takes longer than the user wants, how do I allow them to cancel it?

Upvotes: 5

Views: 7302

Answers (6)

闪电狮
闪电狮

Reputation: 556

Buffering is auto managed by AVPlayer, next line of code indicate AVPlayer to stop buffering.

avplayer.replaceCurrentItem(with: nil)

Upvotes: 0

Svetoslav Bramchev
Svetoslav Bramchev

Reputation: 433

You need to manage preferredForwardBufferDuration of avplayer currentItem. If you want to stop buffering set value to 1 because if you set it to 0 it will be set up automatically

self.avPlayer.currentItem.preferredForwardBufferDuration = 1;

From Apple documentation: This property defines the preferred forward buffer duration in seconds. If set to 0, the player will choose an appropriate level of buffering for most use cases. Setting this property to a low value will increase the chance that playback will stall and re-buffer, while setting it to a high value will increase demand on system resources.

Upvotes: 0

user7301096
user7301096

Reputation: 31

you can try this

// pause buffer
[self.playerItem cancelPendingSeeks];
[self.playerItem.asset cancelLoading];

Upvotes: 3

Sébastien REMY
Sébastien REMY

Reputation: 2468

Swift 3

player?.replaceCurrentItem(with: nil)

Upvotes: 1

justdan0227
justdan0227

Reputation: 1362

So the correct answer is @Che with:

If you will call [self.player replaceCurrentItemWithPlayerItem:nil]; buffering stops. – Che Oct 13 at 14:36

and you do so on the observer listening for the status update from the playerWithPlayerItem. Thanks All!

Upvotes: 9

Ishan Handa
Ishan Handa

Reputation: 2281

I don't think there is a method to directly stop buffering. But you can check the boolean playbackLikelyToKeepUp property of the AVPlayerItem to check if the buffer is loading. I not then pause playback on the AVPlayer.

Upvotes: 1

Related Questions