且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AVPlayer的默认缓冲区大小是多少?

更新时间:2023-09-13 07:54:52

如果您的缓冲区大小表示回放缓冲区,那么我认为该大小没有记载.

If your buffer size means playback buffer, then I believe this size is undocumented.

此行为由AVFoundation提供,引用在 AVFoundation播放中的高级

This behaviour is provided by AVFoundation, quoted on Advances in AVFoundation Playback

我们看到播放开始了,但是随后由于没有足够的缓冲区播放到结尾而很快陷入了停顿.

And we see that playback starts but then we quickly run into a stall because we didn't have enough buffer to play to the end.

在这种情况下,我们将进入等待状态并重新缓冲,直到我们有足够的机会进行游戏为止.

In that case, we'll go into the waiting state and re-buffer until we have enough to play through.

他们只是提到足够而不是确切的时间或规模,这是根据当前网络情况动态衡量的单位,但谁知道呢.

They just mentioned enough not the exact time or size, it make sense that is a dynamic unit of measurement according to current network situation, but who knows.

如果您想控制缓冲,请返回主题. 您可以观察AVPlayerItemloadedTimeRanges属性.

Back to the topic, if you would like to take control of buffering. You can observe AVPlayerItem's loadedTimeRanges property.

下面的代码段具有5秒的缓冲时间:

below code snippet had 5 seconds buffer time:

if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
    
    NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
    if (timeRanges && [timeRanges count]) {
        CMTimeRange timerange = [[timeRanges objectAtIndex:0] CMTimeRangeValue];
        
        if (self.audioPlayer.rate == 0 && !pauseReasonForced) {
            CMTime bufferdTime = CMTimeAdd(timerange.start, timerange.duration);
            CMTime milestone = CMTimeAdd(self.audioPlayer.currentTime, CMTimeMakeWithSeconds(5.0f, timerange.duration.timescale));
            
            if (CMTIME_COMPARE_INLINE(bufferdTime , >, milestone) && self.audioPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay && !interruptedWhilePlaying && !routeChangedWhilePlaying) {
                if (![self isPlaying]) {
                    
                    [self.audioPlayer play];
                }
            }
        }
    }
}