views:

188

answers:

2

I've got 2 methods. One method starts playing an audio file (.mp3), the other method updates a UIToolBar to show a button (PLAY or PAUSE). These two methods are called in the following order:

//Adds some UIBarButtonItems to a UIToolBar
[self togglePlayer];
//Uses AVAudioPlayer
[audioPlayer play];

TogglePlayer does this:

-(void)togglePlayer
{

    NSLog(@"Toggling Player");

    NSArray *barButtonItems;
    UIBarButtonItem *barButtonSpaceL;
    UIBarButtonItem *barButtonSpaceR;
    UIBarButtonItem *barButtonItemPlayer;
    UIBarButtonItem *barButtonItemCancel;
    UIBarButtonItem *barButtonItemLyrics;
    UIBarButtonItem *barButtonItemTweet;

    if([myToolbar.items count] > 0){

        NSEnumerator *enumerator = [myToolbar.items objectEnumerator];
        id object;
        while ((object = [enumerator nextObject])) {

            [object release];
            object = nil;
        }   
    }

    if(!downloadInProgress){

    barButtonSpaceL = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    barButtonSpaceR = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    if(thePlayerState == PLAYER_PLAYING){
    barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"pauseIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pauseButtonPressed:)];
    barButtonItemLyrics = [[UIBarButtonItem alloc] initWithTitle:@"Lyrics" style:UIBarButtonItemStyleBordered target:self action:@selector(switchPageLyrics:)];
    barButtonItemTweet = [[UIBarButtonItem alloc] initWithTitle:@"Tweet This" style:UIBarButtonItemStyleBordered target:self action:@selector(tweetSong:)];

        if(canTweet){
        barButtonItems = [NSArray arrayWithObjects:barButtonItemTweet, barButtonSpaceR, barButtonItemPlayer, barButtonSpaceL, barButtonItemLyrics, nil];
        }else{
        barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemLyrics, nil];         
        }

    }else if(thePlayerState == PLAYER_PAUSED){

    barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"playIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(playButtonPressed:)];
    barButtonItemLyrics = [[UIBarButtonItem alloc] initWithTitle:@"Lyrics" style:UIBarButtonItemStyleBordered target:self action:@selector(switchPageLyrics:)];
    barButtonItemTweet = [[UIBarButtonItem alloc] initWithTitle:@"Tweet This" style:UIBarButtonItemStyleBordered target:self action:@selector(tweetSong:)];


        if(canTweet){
            barButtonItems = [NSArray arrayWithObjects:barButtonItemTweet, barButtonSpaceR, barButtonItemPlayer, barButtonSpaceL, barButtonItemLyrics, nil];
        }else{
            barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemLyrics, nil];         
        }   

    }else{

    //PLAYER OFF    
    barButtonItems = [NSArray arrayWithObjects:barButtonSpaceL, nil];

    }
        [myToolbar setItems:barButtonItems];    

    }else{


        barButtonSpaceL = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        barButtonSpaceR = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        barButtonItemCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"VC1_DownloadCancel", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(downloadCancelled:)];        

        if(thePlayerState == PLAYER_PLAYING){
            barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"pauseIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(pauseButtonPressed:)];
            barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemCancel, nil]; 

        }else if(thePlayerState == PLAYER_PAUSED){
            barButtonItemPlayer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"playIcon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(playButtonPressed:)];
            barButtonItems = [NSArray arrayWithObjects:barButtonItemPlayer, barButtonSpaceR, barButtonItemCancel, nil]; 

        }else{

        //PLAYER OFF
            barButtonItems = [NSArray arrayWithObjects:barButtonSpaceL, barButtonItemCancel, barButtonSpaceR, nil]; 
        }

        [myToolbar setItems:barButtonItems];


    }

}

I call the methods in the above order so that the (pause) button will be shown at the time the song starts playing. But, the problem is that the song starts playing first, and the UIToolBar remains unchanged for quite a while (from 2 to 5 secs) until the button is added and shown.

What I want is for the button to be shown at the same time the song starts playing (i.e. NO DELAY). Is there any way to do this?

A: 

What does [self togglePlayer] actually do?

Alfons
A: 

in order to update the gui you will need to give it a chance to actually update the gui. Since the audio playing happens in a different thread it will begin playing right away. However, since you are probably in an event from gui (say like a touch event) and you haven't returned from that function, the code that responsible for updating the gui hasn't ran yet.

See http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ApplicationEnvironment/ApplicationEnvironment.html#//apple_ref/doc/uid/TP40007072-CH7-SW2 for information on the Cocoa Framework

Using this function to call play should work the way you want. this will place the event to call play after your current event is processed.

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Your example:

//Adds some UIBarButtonItems to a UIToolBar
[self togglePlayer];
//Uses AVAudioPlayer
[audioPlayer performSelector:@SEL(play) withObject:nil afterDelay:0];
Eld