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


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值