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
In this example avPlayer is the AVPlayer instance.
I have built a video control that uses the following:
to position the slider use something like this to get playhead's percentage through the movie, you will need to fire this function repeatedly. So i would run the function like:
float scrubberBarLocation = (scrubberBgImageView.frame.size.width / 100.0f) * [self moviePercentage];
- (float)moviePercentage {
CMTime t1 = [avPlayer currentTime];
CMTime t2 = avPlayer.currentItem.asset.duration;
float myCurrentTime = CMTimeGetSeconds(t1);
float myDuration = CMTimeGetSeconds(t2);
float percent = (myCurrentTime / myDuration)*100.0f;
return percent;
}
Then to update the video I would do something like:
- (void)updateVideoPercent:(float)thisPercent {
CMTime t2 = avPlayer.currentItem.asset.duration;
float myDuration = CMTimeGetSeconds(t2);
float result = myDuration * thisPercent /100.0f;
//NSLog(@"this result = %f",result); // debug
CMTime seekTime = CMTimeMake(result, 1);
[avPlayer seekToTime:seekTime];
}