Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 304
George Avatar asked Sep 20 '11 07:09

George


1 Answers

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!

like image 105
weston Avatar answered Oct 04 '22 23:10

weston