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.


Solution 1:

self.player.currentItem.asset.duration

Got it!

Solution 2:

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