Possible Duplicate:
UISlider to control AVAudioPlayer
I'm trying to develop a simple audio player app for iPhone,i need to implement the progress bar which has to run according to song(mediafile) length.How can i accomplish this?can i use slider with timer? Any help is appreciated in advance, Thank You.
If you're playing your audio with AVPlayer
then you can use its **addPeriodicTimeObserverForInterval**:
method to update your UISlider. For example, if the name of your slider is playerScrubber
:
AVPlayerItem *newPlayerItem = [[AVPlayerItem alloc] initWithAsset:<your asset>];
AVPlayer* player = [[AVPlayer alloc] initWithPlayerItem:newPlayerItem];
CMTime interval = CMTimeMake(33, 1000); // 30fps
id playbackObserver = [player addPeriodicTimeObserverForInterval:interval queue:dispatch_get_current_queue() usingBlock: ^(CMTime time) {
CMTime endTime = CMTimeConvertScale (player.currentItem.asset.duration, player.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
double normalizedTime = (double) player.currentTime.value / (double) endTime.value;
playerScrubber.value = normalizedTime;
}
}];
And later when you're done:
[player removeTimeObserver:playbackObserver];
Goodluck!
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