views:

39

answers:

1

I have an audio player and I want to show the current time of the the playback. I'm using a custom play class.

The app downloads the mp3 to a file then plays from the file when 5% has been downloaded. I have a progress view update as the file plays and update a label on each call to the progress view. However, this is jerky... sometimes even going backward a digit or two.

I was considering using an NSTimer to smooth things out. I would be fired every second to a method and pass the percentage played figure to the method then update the label.

First, does this seem reasonable?

Second, how do I pass the percentage (a float) over to the target of the timer. Right now I am putting the percent played into a dictionary but this seems less than optimal.

This is what is called update the progress bar:

-(void)updateAudioProgress:(Percentage)percent {
    audio = percent;
    if (!seekChanging) slider.value = percent;

    NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];  

    [myDictionary setValue:[NSNumber numberWithFloat:percent] forKey:@"myPercent"];


    [NSTimer scheduledTimerWithTimeInterval:5
                                     target:self 
                                   selector:@selector(myTimerMethod:) 
                                   userInfo:myDictionary 
                                    repeats:YES];
    [myDictionary release];
}

This is called first after 5 seconds but then updates each time the method is called.

As always, comments and pointers appreciated.

A: 

UserInfo is a void *, so you could just pass an int for percentage directly, or just a pointer to your NSNumber object; it doesn't have to be an NSDictionary. Apart from that, using a dictionary is a standard approach, and doesn't have excessive overhead.

Note that you are creating a new timer with a 5 second repeat every time this method is called, which may or may not be your intention. When you create repeating timers (or normal ones) you should make sure to invalidate them when not required.

Paul Lynch
Thank you for your answer Paul.
Michael