0

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.

Dylan Gattey
  • 1,713
  • 13
  • 34

2 Answers2

3

Did you check your IBAction is being called (perhaps with NSLog)? If not, make sure you set the delegate of the Slider.

Otherwise, check my answer to this question which is very similar.

Community
  • 1
  • 1
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Your other answer doesn't work with regards to `MPMusicPlayerController`. It's a different syntax and you can't easily set the value to the current time. See my conversions above. The "not working" is that the slider value doesn't change through playment of the song, and when the slider value is manually changed, it stops the music. So therefore it's being called, just not with the right result. I think it's something to do with my conversions, but I can't figure out what. I've tried rewriting it twice with no success. I'll go check the delegate right now. That might be it. Stupid if it is :) – Dylan Gattey Aug 24 '11 at 22:20
  • 1
    Not it, because a `UISlider` doesn't have a delegate object... But thanks for trying. – Dylan Gattey Aug 24 '11 at 22:22
  • Aha. Thanks for pointing me in the right direction. I figured out that the current time increases by about 1 each second that the `updateSliderTime` is called. This makes sense and it means that the method IS being called, but the IBAction still stops the music. Also, the value from the duration property is consistently above 10000, which doesn't make sense. Any thoughts about what to do to make that value the true value? – Dylan Gattey Aug 24 '11 at 22:37
1

Figured it out. Everything was actually working how it should except setting the maximum value for the slider. I converted it oddly. Instead of:

self.currentlyPlayingTotalDuration = [[NSNumber alloc] initWithFloat:(int)[NSValue valueWithPointer:[self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration]]];

I should be using:

self.currentlyPlayingTotalDuration = [self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration];

The value is already a number. Thanks to Mundi for motivating me to log the value. When I did, I realized it was already a float. Silly me :)

Dylan Gattey
  • 1,713
  • 13
  • 34