How to make the Progressbar(UISlider) work dynamically in my app? [duplicate]
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!
I'd say yes, a UISlider is your best friend. That's what iPod uses, too. You'll have to modify t a bit to indicate the progress visually. Make a timer that polls the position of your playback and updates the slider's value accordingly. You might make the slider's minimum value 0 and the maximum value the time of the playback in seconds.
Have a look at this post here on Stack Overflow, it has a pretty good demonstration: UISlider with ProgressView combined
If you use AVAudioPlayer then it has a property called currentTime. You can assign its value to the UIProgressBar value like:
progressBar.value=myPlayer.currentTime;
You'll need to set up a timer to constantly update the progress.
精彩评论