使用AVPlayer获取HTTP live stream audio文件的duration

来源:互联网 发布:pdfobject.js 兼容ie 编辑:程序博客网 时间:2024/04/19 12:10


关于AVPlayer的详细解析,请看Apple document。但是document里面并没有详细解析如何获取HTTP live stream文件的duration,google了一下,发现通过duration属性获取的方法,不能得到正确的直。正确的做法应该是通过调用AVPlayerItem的seekableTimeRanges来获取(loadedTimeRagnes表示已经下载的部分)。

事例代码如下:

    [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(2, 1) queue:nil usingBlock:^(CMTime time){         AVPlayerItem* currentItem = self.player.currentItem;        NSArray* loadedRanges = currentItem.seekableTimeRanges;        if (loadedRanges.count > 0)        {            CMTimeRange range = [[loadedRanges objectAtIndex:0] CMTimeRangeValue];            Float64 duration = CMTimeGetSeconds(range.start) + CMTimeGetSeconds(range.duration);            NSLog(@"duration:%g", duration);                        CMTime currentTime = self.player.currentItem.currentTime;                        self.slider.value = CMTimeGetSeconds(currentTime) / duration;        }    }];