如何获得video全部持续时间和当前播放时间?
我需要使用swift创build一个自定义的video插件。 但我不知道如何获得video的全部时间和当前的播放时间。 在我的控制台刚出现这个输出, C.CMTime
。 我不知道我的代码有什么问题。
我的代码
let url = NSBundle.mainBundle().URLForResource("Video", withExtension:"mp4") let asset = AVURLAsset(URL:url, options:nil) let duration: CMTime = asset.duration println(duration)
请指教。 谢谢。
您可以使用CMTimeGetSeconds将CMTime转换为秒。
let durationTime = CMTimeGetSeconds(duration)
使用ios Objective c概念
- (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)); } }