Mert Dümenci
Mert Dümenci

Reputation: 513

Music progress bar

I am currently writing a music app based on Apple's one, and I want to add a progress bar to it.

How can I make a progress bar to show the progress of a music that is playing in the default iOS's iPod app on my app?

Upvotes: 2

Views: 4444

Answers (4)

max_
max_

Reputation: 24481

Add a timer to fire every second (1.0) and get it to call an method to update the progress bar like the one below:

- (void) updateProgressBar:(NSTimer *) timer {
    if (currentPlaybackTime != lengthOfSong) {
        [self.progressBar setValue:self.musicPlayer.currentTime];
    }
    else {
        [timer invalidate], timer = nil;
        [self.progressBar setValue:lengthOfSong];
    }
}

Upvotes: 2

Frankrockz
Frankrockz

Reputation: 604

This is how I accomplised this In the header file I declared these variables.

NSTimer * currentTimeUpdateTimer;

'UISlider *timeSlider;

BOOL userIsScrubbing;

- (IBAction)handleScrubberTouchDown:(id)sender;

- (IBAction)handleScrubberTouchUp:(id)sender;

- (IBAction)handleScrub:(id)sender;

In the main file now under the viewDidLoad method, I ran an NSTimer currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateCurrentiPodItemTime) userInfo:NULL repeats:YES];

userIsScrubbing = NO;

After that, it was a matter of updating everything.

- (void)updateCurrentiPodItemTime {
    MPMediaItem *nowPlayingItem = [self.musicPlayer nowPlayingItem];

if (nowPlayingItem == nil) {
    self.minLabel.text = @"00:00";

} else {
    NSNumber *duration = [nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration];
    double currentTime = self.musicPlayer.currentPlaybackTime;
    self.minLabel.text = [NSString stringWithFormat: @"%02d:%02d", (int) currentTime / 60, (int) currentTime % 60];     
    self.maxLabel.text = [NSString stringWithFormat: @"%02d:%02d", [duration intValue] / 60, [duration intValue] % 60];

    if (!userIsScrubbing)
        self.timeSlider.value = (float) currentTime;
    }
}

- (IBAction)handleScrubberTouchDown:(id)sender {
     userIsScrubbing = YES;
}

- (IBAction)handleScrubberTouchUp:(id)sender {
    userIsScrubbing = NO;
}

- (IBAction)handleScrub:(id)sender {
    self.musicPlayer.currentPlaybackTime = timeSlider.value;
}

Upvotes: 3

progrmr
progrmr

Reputation: 77191

Add a UIProgressView to your view and update it periodically.

Upvotes: 0

Kristian
Kristian

Reputation: 1389

You probably want to take a look at the MPMusicPlayerController class reference — specifically the currentPlayBackTime property. Never played with it, but looks like you want to take that and render it somehow in the interface.

Upvotes: 3

Related Questions