2

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.

4

1 回答 1

0

查看 AVPlayerItem 上的preferredForwardBufferDuration属性。

像这样的东西应该工作:

AVPlayerItem* theItem = self.player.currentItem;
theItem.preferredForwardBufferDuration = CMTimeGetSeconds( theItem.duration );

然后您可以观察播放缓冲区已满,就像您在代码中显示的那样,开始播放。

于 2017-05-02T18:45:06.710 回答