Setup
I'm currently developing a music app and I'm having problems with the progress bar slider.
self.musicPlayer
is the [MPMusicPlayerController applicationMusicPlayer]
self.currentlyPlayingTotalDuration
is an NSNumber
self.currentlyPlayingTimeSlider
is a UISlider
Overview
The below code is executed when a new song is picked in the MPMusicPlayerController
. In this method (not all of which is shown), I set my music player to play the picked song (working) and play it (working). Then I set the total duration of the song to the maximum value of the slider (working)
self.currentlyPlayingTotalDuration = [[NSNumber alloc] initWithFloat:(int)[NSValue valueWithPointer:[self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration]]];
[self.currentlyPlayingTimeSlider setMaximumValue:self.currentlyPlayingTotalDuration.floatValue];
I've logged the values to see if the values are setting correctly (they are)
NSLog(@"Number:%@", [NSNumber numberWithFloat:(int)[NSValue valueWithPointer:[self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration]]]);
NSLog(@"Max:%f", self.currentlyPlayingTimeSlider.maximumValue);
Log output is below, looking as it should
2011-08-24 13:38:25.004 App [1241:707] Number:1107392
2011-08-24 13:38:25.009 App [1241:707] Max:1400544.000000
And then I set my currentlyPlayingTimeSlider
value to 0 to start (working)
self.currentlyPlayingTimeSlider.value = 0.0;
Next, I set my updateSliderTime
method to call once per second to move the progress bar farther by one second (working)
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSliderTime:) userInfo:nil repeats:YES];
Problems
The full method code is below for the updateSliderTime
method, which is not working, even though it is being called every second
- (void)updateSliderTime:(NSTimer *)timer {
[self.currentlyPlayingTimeSlider setValue:[[NSNumber numberWithDouble:self.musicPlayer.currentPlaybackTime] floatValue]];
}
Also, the method I call for a valueChanged
event that I've setup in Interface Builder is below. It also is not working
- (IBAction)sliderChanged:(id)sender {
[self.musicPlayer setCurrentPlaybackTime:[[NSNumber numberWithFloat:self.currentlyPlayingTimeSlider.value] doubleValue]];
}
Can anyone help me figure out what I'm doing wrong? It has to be something in these last two methods, as the first one calls fine. I do no other customization/messing with the slider anywhere else.