5

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.

Community
  • 1
  • 1
George
  • 352
  • 2
  • 5
  • 15

3 Answers3

30

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!

Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75
weston
  • 2,878
  • 3
  • 24
  • 23
  • 1
    Works but answer is outdated please update as "dispatch_get_current_queue()" is deprecated since iOS 6 – iPhone 7 Apr 13 '15 at 10:45
0

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

Community
  • 1
  • 1
Krumelur
  • 32,180
  • 27
  • 124
  • 263
0

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.

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
  • 1
    `currentTime` works for `AVAudioPlayer`, but not for `AVPlayer`. – Neeku Nov 26 '13 at 09:03
  • That's why I said `If you use AVAUdioPLayer`. I can't see anywhere me stating that currentTime is applicable for both cases. – Mikayil Abdullayev Aug 21 '14 at 12:14
  • Right @Mike. I see I've commented on this last year when I was stuck with `AVPlayer` myself. However it's not me who has down-voted you on this recently. Həmdə həmvətənıq eləbir! – Neeku Aug 21 '14 at 13:17
  • Actually I think my comment was because you have `myPlayer` rather than `myAudioPlayer` which can be misleading due to naming convensions. – Neeku Aug 21 '14 at 13:22
  • Hmmm, the way I code is you either have AVPlayer or AVAudioPlayer. Hardly anyone will use both one for sound output which would cause the misleading you talk about. Don't you find it awkward? – Mikayil Abdullayev Aug 22 '14 at 04:40
  • I think I was shifting back and forth from `AVPlayer` to `AVAudioPlayer` at that point, because each of them had their own advantages and disadvantages. But my comment has only pointed out that it works with `AVAudioPlayer`, so that in case someone is using `AVPlayer` they wouldn't get confused about it. (: – Neeku Aug 22 '14 at 09:46