AVPlayer / AVPlayerItem的缓冲区大小

8
我正在为iOS创建一个流媒体广播应用程序,希望通过调整AVPlayer和AVPlayerItem的属性,在不稳定的网络连接条件下获得更可靠的播放效果。 我想增加缓冲区大小。
我找到的唯一答案是这里
有没有不需要使用OpenAL就能实现这一点的方法?

请参考这个答案:https://dev59.com/1m855IYBdhLWcg3wp2J0#39036307 这可能对你有所帮助。 - iPatel
2个回答

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://dev59.com/8Gsz5IYBdhLWcg3wv6fu#7730708


1
我无法理解改变“5”如何改变玩家实际缓冲的量?请解释。 - dev
1
你能解释一下吗?你正在更新哪个值? - DivineDesert
这可能会有所帮助:https://dev59.com/rm855IYBdhLWcg3wKxDc - arunit21
totalDuration = avplayerItem.duration; 是视频的总时长。 duration = 播放器当前位置。 smartValue = 我们在缓冲区中拥有的视频总秒数。 smartValue - duration > 5 = 这表示我们在缓冲区中有超过5秒的视频。 - arunit21
smartValue如何表示已加载的持续时间,如果它包括CMTimeRange.start?CMTimeRange.start可能是300秒,而CMTimeRange.duration是20秒。您确定总缓冲区大小将是320秒吗? - Dmitriy

2
请看这里:AVPlayer streaming progressHow 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];
    }

10
你的回答告诉我如何观察/检索缓冲区,但没有告诉我如何增加缓冲区大小。 - Ospho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接