Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Duration from AVPlayer (Not AVAudioPlayer)?

I would like to make a UISlider(scrubber) for my AVPlayer. But since this is not an AVAudioPlayer, it doesn't have a built in duration. Any suggestion on how to create the Slider for fast forward, rewind and progress of the playback?

I read the doc on AVPlayer, it has a built in seekToTime or seekToTime:toleranceBefore:toleranceAfter:. I don't really understand it. Would this be the answer for my slider? AVPlayer also has addPeriodicTimeObserverForInterval:queue:usingBlock:, is this for getting the duration of my track? Can someone give me an example on how to implement this code? I am not a fan of Apple's documentation. It seems very hard to understand.

like image 746
slowman21 Avatar asked Oct 19 '10 20:10

slowman21


3 Answers

self.player.currentItem.asset.duration 

Got it!

like image 148
slowman21 Avatar answered Sep 28 '22 17:09

slowman21


headers

#import <AVFoundation/AVPlayer.h> #import <AVFoundation/AVPlayerItem.h> #import <AVFoundation/AVAsset.h> 

code

CMTime duration = self.player.currentItem.asset.duration; float seconds = CMTimeGetSeconds(duration); NSLog(@"duration: %.2f", seconds); 

frameworks

AVFoundation CoreMedia 
like image 32
neoneye Avatar answered Sep 28 '22 16:09

neoneye


For Swift to get duration in seconds

if let duration = player.currentItem?.asset.duration {
    let seconds = CMTimeGetSeconds(duration)
    print("Seconds :: \(seconds)")
}
like image 21
Hardik Thakkar Avatar answered Sep 28 '22 18:09

Hardik Thakkar