Im trying to fully load a video URL into an AVPlayer before the user is able to play the video, for seamless playback.
I add two observers to the AVPlayerItem
:
[item addObserver:self forKeyPath:@"loadedTimeRanges"
options:NSKeyValueObservingOptionNew context:&timeRanges];
[item addObserver:self forKeyPath:@"playbackBufferFull"
options:NSKeyValueObservingOptionNew context:&playbackBufferFull];
I wait until value is equal to the duration of the video - fully loaded.
if (context == &timeRanges) {
NSLog(@"BUFFERING");
NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
CMTimeRange timerange = [timeRanges[0] CMTimeRangeValue];
CGFloat value = CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration));
CGFloat duration = CMTimeGetSeconds(self.player.currentItem.duration);
NSLog(@"VALUE:DURATION == %f:%f", value, duration);
if(value == duration) {
NSLog(@"FINISHED");
[self prerollVideoPlayer];
}
return;
}
But this rarely happens. The player usually stops buffering at around 3 for a five second video. Then the "playbackBufferFull" is called
if (context == &playbackBufferFull){
if (self.player.currentItem.playbackBufferFull) {
NSLog(@"IS FULL");
//[self prerollVideoPlayer];
}
return;
}
How can I increase the buffer size? Or how to buffer the rest of the video once playbackBufferFull
is called? The videos being played, are only five seconds long.