AVPlayer / AVPlayerItem的缓冲区大小

时间:2014-01-28 08:40:13

标签: ios objective-c avaudioplayer avplayer

我正在为iOS创建流媒体广播应用程序,我想调整AVPlayer和AVPlayerItem的属性,以便在有损连接条件下让我更可靠地播放。 我想增加缓冲区。

我能找到的唯一答案是here

无论如何都可以在不进入OpenAL的情况下实现这一目标吗?

2 个答案:

答案 0 :(得分:11)

在观察者方法中添加以下代码。

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
}

我已经实施并且运作良好。

参考:https://stackoverflow.com/a/7730708/2315453

答案 1 :(得分:2)

见这里:AVPlayer streaming progress

在这里:How to get file size and current file size from NSURL for AVPlayer iOS4.0

您可以观察播放器的属性“currentitem.loadedTimeRanges”,并且在抛出事件时,您可以在开始播放之前检查缓冲的数量。以下是我如何使用它的示例:

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