开发者

How do I force one method to be executed before another method?

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    
  开发者_JAVA技巧  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?


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];


Another approach could be to prepare the system to play your audio file, using

[audioPlayer prepareToPlay];

somewhere in your initialisation code, e.g. in viewDidLoad. This way everything will be set once you want the audio to play, and the play method will not cause a significant delay. (not in your user interface code, and not in actual playback)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜