What is the default buffer size of AVPlayer?

两盒软妹~` 提交于 2020-06-25 09:44:29

问题


What is the default maximum and minimum buffer size of iOS AVPlayer? How to increasing its size for mp3 streaming? thank you


回答1:


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

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.

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

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];
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/25397719/what-is-the-default-buffer-size-of-avplayer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!