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

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

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

@property (nonatomic,readonly) NSTimeInterval playableDuration

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

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

提前致谢.

解决方法

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.

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

原文链接:https://www.f2er.com/iOS/332901.html

猜你在找的iOS相关文章