2

In Xcode, I have set my buttons to play a music clip which will last 40 seconds. My question is how do I link up a UIProgressView to the music playing? For example, if the song is half way through, the progress bar will display that.

Undo
  • 25,519
  • 37
  • 106
  • 129
  • How are you playing the music? AVAudioPlayer has a currentTime and duration which you could use to calculate the value to pass to UIProgressView. – Lewis Gordon May 06 '13 at 18:17
  • Yes I am using the avaudioplayer, could you give me a short explain of linking it up to the player? I have tried but gives lots of errors? – Jack Rhodes May 06 '13 at 23:10

1 Answers1

3

If you have the following defined in your class:

AVAudioPlayer *audioPlayer;
UIProgressView *progressView;
NSTimer *audioTimer;

Running it off a timer seems to work:

- (void)audioProgressUpdate
{
    if (audioPlayer != nil && audioPlayer.duration > 0.0)
        [progressView setProgress:(audioPlayer.currentTime / audioPlayer.duration)];
}

When you start the clip start the timer (this runs it every tenth of a second):

[audioPlayer play];
audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(audioProgressUpdate) userInfo:nil repeats:YES];

And when you stop the clip, stop the timer:

[audioTimer invalidate];
[audioPlayer stop];
Lewis Gordon
  • 1,711
  • 14
  • 19