Question

I'm writing an iPod app replacement, and in my "now playing" view I want to have a progress indicator like the iPod app, that shows current position, and allows the user to drag to change the play position.

How do I do that?

Do I use a UISlider and UIProgressView?

Was it helpful?

Solution

Omitted some things here and there, but it's enough to plug in.

// .h

IBOutlet UISlider *progressSlider;
NSTimer *progressTimer;

@property (nonatomic, retain) UISlider *progressSlider;
@property (nonatomic, retain) NSTimer *progressTimer;

- (void)updateSlider;
- (void)resetTimer:(NSTimer *)timer;


// .m

// synthesize variables

- (void)handleNowPlayingItemChanged:(id)notification {
    NSNumber *duration = [item valueForProperty:MPMediaItemPropertyPlaybackDuration];
    float totalTime = [duration floatValue];
    progressSlider.maximumValue = totalTime;
}

- (void)updateSlider {
    [progressSlider setValue:musicPlayer.currentPlaybackTime animated:YES];
}

- (void)resetTimer:(NSTimer *)timer {
    [progressTimer invalidate];
    progressTimer = nil;
    progressTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
                                            selector:@selector(updateSlider)
                                            userInfo:nil repeats:YES];
}

// init and release methods (together at the bottom with view controls
//                                  (like viewDidLoad) for convenience)
//
// don't forget to register for notifications

@end

If you want a sample app (device only; iPod library), I can write one, but it won't be much more than this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top