iOS – 如何获得AVPlayer的可播放持续时间

前端之家收集整理的这篇文章主要介绍了iOS – 如何获得AVPlayer的可播放持续时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
MPMoviePlayerController有一个名为playableDuration的属性.

playableDuration The amount of currently playable content (read-only).@H_301_6@

@property (nonatomic,readonly) NSTimeInterval playableDuration@H_301_6@

For progressively downloaded network content,this property reflects
the amount of content that can be played now.@H_301_6@

AVPlayer有类似的东西吗?
我在Apple Docs或Google中找不到任何内容(在Stackoverflow.com上也没有找到)@H_301_6@

提前致谢.@H_301_6@

解决方法

playableDuration可以通过以下过程粗略地实现:
- (NSTimeInterval) playableDuration
{
//  use loadedTimeRanges to compute playableDuration.
AVPlayerItem * item = _moviePlayer.currentItem;

if (item.status == AVPlayerItemStatusReadyToPlay) {
    NSArray * timeRangeArray = item.loadedTimeRanges;

    CMTimeRange aTimeRange = [[timeRangeArray objectAtIndex:0] CMTimeRangeValue];

    double startTime = CMTimeGetSeconds(aTimeRange.start);
    double loadedDuration = CMTimeGetSeconds(aTimeRange.duration);

    // FIXME: shoule we sum up all sections to have a total playable duration,// or we just use first section as whole?

    NSLog(@"get time range,its start is %f seconds,its duration is %f seconds.",startTime,loadedDuration);


    return (NSTimeInterval)(startTime + loadedDuration);
}
else
{
    return(CMTimeGetSeconds(kCMTimeInvalid));
}
}

_moviePlayer是您的AVPlayer实例,通过检查AVPlayerItem的loadedTimeRanges,您可以计算估计的playableDuration.@H_301_6@

对于只有1秒的视频,您可以使用此程序;但对于多节视频,您可能需要检查loadedTimeRagnes数组中的所有时间范围以获得正确答案.@H_301_6@

猜你在找的iOS相关文章