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