Reputation: 21
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.
Upvotes: 2
Views: 1895
Reputation: 1731
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];
Upvotes: 3