xcode iOS - 如何获得 AVPlayer 的可播放时长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6815316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - How can i get the playable duration of AVPlayer
提问by MystyxMac
The MPMoviePlayerController has a property called playableDuration.
MPMoviePlayerController 有一个名为 playableDuration 的属性。
playableDurationThe 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.
playableDuration当前可播放内容的数量(只读)。
@property (nonatomic, readonly) NSTimeInterval playableDuration
对于渐进式下载的网络内容,此属性反映了现在可以播放的内容量。
Is there something similar for AVPlayer? I can't find anything in the Apple Docs or Google (not even here at Stackoverflow.com)
AVPlayer 有类似的东西吗?我在 Apple Docs 或 Google 中找不到任何内容(甚至在 Stackoverflow.com 上也找不到)
Thanks in advance.
提前致谢。
回答by John Lingburg
playableDuration can be roughly implemented by following procedure:
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 is your AVPlayer instance, by checking AVPlayerItem's loadedTimeRanges, you can compute a estimated playableDuration.
_moviePlayer 是您的 AVPlayer 实例,通过检查 AVPlayerItem 的加载时间范围,您可以计算出估计的可播放持续时间。
For videos that has only 1 secion, you can use this procedure; but for multi-section video, you may want to check all time ranges in array of loadedTimeRagnes to get correct answer.
对于只有 1 节的视频,您可以使用此程序;但是对于多节视频,您可能需要检查loadedTimeRagnes 数组中的所有时间范围以获得正确答案。
回答by Syed Ali Salman
all you need is
所有你需要的是
self.player.currentItem.asset.duration
simply best
最好的
回答by Corey Floyd
Building on John's Answer…
以约翰的回答为基础……
This is the apparent default behavior of Apple players: "Show the Max Time of the playable range that encloses the current time"
这是 Apple 播放器的明显默认行为:“显示包含当前时间的可玩范围的最大时间”
- (NSTimeInterval)currentItemPlayableDuration{
// use loadedTimeRanges to compute playableDuration.
AVPlayerItem * item = self.audioPlayer.currentItem;
if (item.status == AVPlayerItemStatusReadyToPlay) {
NSArray * timeRangeArray = item.loadedTimeRanges;
CMTime currentTime = self.audioPlayer.currentTime;
__block CMTimeRange aTimeRange;
[timeRangeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
aTimeRange = [[timeRangeArray objectAtIndex:0] CMTimeRangeValue];
if(CMTimeRangeContainsTime(aTimeRange, currentTime))
*stop = YES;
}];
CMTime maxTime = CMTimeRangeGetEnd(aTimeRange);
return CMTimeGetSeconds(maxTime);
}
else
{
return(CMTimeGetSeconds(kCMTimeInvalid));
}
}
}
回答by leviathan
You will have to detect when the AVPlayer is ready to play your media file. Let me know, if you don't know how to do this.
您必须检测 AVPlayer 何时准备好播放您的媒体文件。如果您不知道如何执行此操作,请告诉我。
However, once the media file is loaded, you can use this method:
但是,一旦加载了媒体文件,您就可以使用此方法:
#import <AVFoundation/AVFoundation.h>
/**
* Get the duration for the currently set AVPlayer's item.
*/
- (CMTime)playerItemDuration {
AVPlayerItem *playerItem = [mPlayer currentItem];
if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
return [[playerItem asset] duration];
}
return(kCMTimeInvalid);
}
When you use this method its important to understand (because you're streaming content) that the length value may be invalid or something. So you must check this before using it for processing.
当您使用此方法时,重要的是要了解(因为您正在流式传输内容)长度值可能无效或其他内容。所以你必须在使用它进行处理之前检查这一点。
CMTime playerDuration = [self playerItemDuration];
if (CMTIME_IS_INVALID(playerDuration)) {
return;
}
double duration = CMTimeGetSeconds(playerDuration);
回答by Michal Zaborowski
Swift version of closes playable duration:
关闭可玩持续时间的 Swift 版本:
var playableDuration: TimeInterval? {
guard let currentItem = currentItem else { return nil }
guard currentItem.status == .readyToPlay else { return nil }
let timeRangeArray = currentItem.loadedTimeRanges
let currentTime = self.currentTime()
for value in timeRangeArray {
let timeRange = value.timeRangeValue
if CMTimeRangeContainsTime(timeRange, currentTime) {
return CMTimeGetSeconds(CMTimeRangeGetEnd(timeRange))
}
}
guard let timeRange = timeRangeArray.first?.timeRangeValue else { return 0}
let startTime = CMTimeGetSeconds(timeRange.start)
let loadedDuration = CMTimeGetSeconds(timeRange.duration)
return startTime + loadedDuration
}