How to get the full duration of the video and the current playback time?

I need to create my own video plugin using swift. But I do not know how to get the full duration of the video and the current playback time. In my console only I did this conclusion C.CMTime. I am not sure what is wrong with my code.

My code

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

println(duration)
+5
source share
2 answers

You can use CMTimeGetSeconds to convert CMTime to seconds.

let durationTime = CMTimeGetSeconds(duration)
+6
source

Using the ios Objective c concept

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

}
0
source

All Articles