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

问题描述 投票:5回答:2

我需要使用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)
ios swift video
2个回答
6
投票

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

let durationTime = CMTimeGetSeconds(duration)

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

}
© www.soinside.com 2019 - 2024. All rights reserved.