Buffer size for AVPlayer / AVPlayerItem

12,851

Solution 1

Add the below piece of code in your observer 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 have implemented and works good.

Referred from : https://stackoverflow.com/a/7730708/2315453

Solution 2

See here: AVPlayer streaming progress

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

You can observer the property "currentitem.loadedTimeRanges" of the player, and when the events are thrown, you can check how much was buffered, before beginning play back. 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];
    }
Share:
12,851

Related videos on Youtube

Ospho
Author by

Ospho

Updated on September 15, 2022

Comments

  • Ospho
    Ospho over 1 year

    I'm creating a streaming radio application for iOS and I would like to tweak the properties of AVPlayer and AVPlayerItem to give me more reliable playback in lossy connectivity conditions. I would like to increase the buffersize.

    The only answer I could find is here

    Is there anyway to achieve this without going to OpenAL?

  • Ospho
    Ospho about 10 years
    Your answer shows me how to observe/retreive the buffer, but it does not tell me how to increase the buffer size.
  • dev
    dev over 9 years
    I cannot understand how changing "5" would change how much the player actually buffers? Please explain
  • DivineDesert
    DivineDesert over 9 years
    Can you explain this ? Which value are you updating ?
  • arunit21
    arunit21 over 9 years
  • arunit21
    arunit21 over 9 years
    totalDuration = avplayerItem.duration; is the total video duration time. duration = Player heads current position. smartValue = Total seconds of video we have in buffer. smartValue - duration > 5 = This states that we have 5 seconds+ video in buffer.
  • Dmitriy
    Dmitriy almost 6 years
    How could smartValue represent the loaded duration it it includes CMTimeRange.start? CMTimeRange.start could be 300 seconds and CMTimeRange.duration 20 seconds. Are you sure total buffer size would be 320 seconds?