Buffer size for AVPlayer / AVPlayerItem

百般思念 提交于 2021-02-05 13:10:00

问题


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?


回答1:


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




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


来源:https://stackoverflow.com/questions/21400591/buffer-size-for-avplayer-avplayeritem

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