Buffer Size for AVPlayer / AVPlayerItem

I am creating a streaming radio application for iOS, and I would like to set the properties of AVPlayer and AVPlayerItem to give me more reliable playback in a lossy environment. I would like to increase buffering.

The only answer I could find is here

Is it possible to achieve this without leaving OpenAL?

+8
ios objective-c avaudioplayer avplayer
source share
2 answers

Add the following code snippet to your observation method.

NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey]; CMTimeRange timerange = [timeRanges[0] CMTimeRangeValue]; CGFloat smartValue = CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration)); CGFloat duration = CMTimeGetSeconds(self.player.currentTime); if(smartValue - duration > 5 || smartValue == duration) { // Change the value "5" to your needed secs, its the buffer size. // Play the video } 

I implemented and works well.

Referrals: stack overflow

+10
source share

See here: AVPlayer progress for streaming

And here: How to get file size and current file size from NSURL for AVPlayer iOS4.0

You can watch the player’s currentitem.loadedTimeRanges property, and when events are thrown, you can check how much has been buffered before starting playback. Here is an example of how I use it:

 #define VIDEO_BUFFER_READY_PERCENT 0.3 - (void)viewDidLoad{ [super viewDidLoad]; [self.player addObserver:self forKeyPath:@"currentItem.loadedTimeRanges" options:NSKeyValueObservingOptionNew context:&kTimeRangesKVO]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == &kTimeRangesKVO) { float percent = CMTimeGetSeconds(timerange.duration) / CMTimeGetSeconds(self.player.currentItem.duration); if (percent > VIDEO_BUFFER_READY_PERCENT) { NSLog(@" . . . %.5f -> %.5f, %f percent", CMTimeGetSeconds(timerange.duration), CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration)), percent); [self.player prerollAtRate:0.0 completionHandler:^(BOOL finished) { [self.player seekToTime:kCMTimeZero]; } } else{ [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } 
+1
source share

All Articles