Nurdin
Nurdin

Reputation: 23883

How to get video full duration and current playing time?

I need to create a custom video plugin using swift. But I don't know how to get video full duration and current playing time. In my console just appeared this output, C.CMTime. I'm not sure what 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)

Upvotes: 5

Views: 3560

Answers (2)

NSDeveloper
NSDeveloper

Reputation: 1640

You can use CMTimeGetSeconds to converts a CMTime to seconds.

let durationTime = CMTimeGetSeconds(duration)

Upvotes: 6

Rohit suvagiya
Rohit suvagiya

Reputation: 1015

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

}

Upvotes: 0

Related Questions