8

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?

Community
  • 1
  • 1
Ospho
  • 2,756
  • 5
  • 26
  • 39
  • Follow this answer. http://stackoverflow.com/questions/4218090/pre-buffering-for-avqueueplayer/39036307#39036307 Might be helpful for you. – iPatel Aug 19 '16 at 10:39

2 Answers2

11

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

Community
  • 1
  • 1
arunit21
  • 670
  • 1
  • 11
  • 25
  • 1
    I cannot understand how changing "5" would change how much the player actually buffers? Please explain – dev Dec 01 '14 at 19:57
  • 1
    Can you explain this ? Which value are you updating ? – DivineDesert Jan 01 '15 at 19:18
  • http://stackoverflow.com/questions/4495433/uislider-with-progressview-combined this might help. – arunit21 Jan 19 '15 at 11:43
  • 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. – arunit21 Jan 19 '15 at 11:52
  • 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? – Dmitriy Jun 09 '18 at 08:22
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];
    }
Community
  • 1
  • 1
cohen72
  • 2,830
  • 29
  • 44
  • 10
    Your answer shows me how to observe/retreive the buffer, but it does not tell me how to increase the buffer size. – Ospho Jan 28 '14 at 22:11