如何获得视频完整持续时间和当前播放时间?

时间:2015-07-01 09:28:12

标签: ios swift video

我需要使用swift创建自定义视频插件。但我不知道如何获得视频完整持续时间和当前播放时间。在我的控制台中出现了这个输出C.CMTime。我不确定我的代码有什么问题。

我的代码

let url = NSBundle.mainBundle().URLForResource("Video", withExtension:"mp4")
let asset = AVURLAsset(URL:url, options:nil)
let duration: CMTime = asset.duration

println(duration)

2 个答案:

答案 0 :(得分:6)

您可以使用CMTimeGetSeconds将CMTime转换为秒。

let durationTime = CMTimeGetSeconds(duration)

答案 1 :(得分:0)

使用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));
  }

}