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.
self.player.currentItem.asset.duration
Got it!
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
For Swift to get duration in seconds
if let duration = player.currentItem?.asset.duration {
let seconds = CMTimeGetSeconds(duration)
print("Seconds :: \(seconds)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With