如何获取视频的完整时长和当前播放时间?

5

我需要使用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个回答

6
您可以使用CMTimeGetSeconds将CMTime转换为秒。
let durationTime = CMTimeGetSeconds(duration)

我收到了这个错误信息:'Float64' 无法转换为 'CMTime'。 - Nurdin
为什么你要再次将Float64转换为CMTime? - NSDeveloper

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

}

抱歉,我不了解Swift语言。 - Rohit suvagiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接