开发者

How to prevent AVAudioPlayer from playing before a view is displayed

开发者 https://www.devze.com 2023-04-05 05:35 出处:网络
Hi there I have an a UIView named HomeViewController that use UINavigationController.On a click of a button, I push another view using the following code:

Hi there I have an a UIView named HomeViewController that use UINavigationController. On a click of a button, I push another view using the following code:

gameView = [[GameViewControler alloc] init];
    [[self navigationController] pushViewController:gameView animated:YES];
    [gameView release];

In GameViewController's viewDidLoad, I use the following code to play two audio file one after another:

//handle sound stuff

if([[GameStore defaultStore] currentIndex] == 0)
{
    if(![audioPlayer isPlaying])
    {
        NSString *findPath = [[NSBundle mainBundle] pathForResource:@"findtheword" ofType:@"aiff"];
        if(findPath)
        {
            NSURL *findURL = [NSURL fileURLWithPath:findPath];
            NSError *findError;
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:findURL error:&findError];

            if(audioPlayer == nil)
            {
                NSLog([findError description]);
            }
            else{
                [audioPlayer play];
            }
        }
    }
}

while ([audioPlayer isPlaying]) {
    //do nothing and wait so the sound is not overlap
}


if(![audioPlayer isPlaying] || ([audioPlayer isPlaying] && override)){
    Word *currentWord = [[GameStore defaultStore] currentWord];
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:[currentWord fileName]
                                                      ofType:[currentWord fileType]];
    // If this file is actually in the bundle...
    if (soundPath) {
        // Create a file URL with this path
        NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];

        if (audioPlayer == nil)
            NSLog([error description]);
        else
            [audioPlayer play];
    }
    [FlurryAnalytics logEvent:@"GameViewController - playSound"];
}
else{
    [FlurryAnalytics logEvent:@"GameViewController - playSound is still playing"];
}
开发者_JAVA百科

I can't figure out why the first audio file started to play while the screen is still on HomeViewController even though it is being called from GameViewController's viewDidLoad.

If I remove the following code:

while ([audioPlayer isPlaying]) {
    //do nothing and wait so the sound is not overlap
}

The first audio file started to play when GameViewController is loaded but the 2nd audio file overlap.

Thank you very much.


Does the audio file start to play after you click some button or table view cell in HomeViewController? As in, it starts to play while the GameViewController's view is being animated from right to left? If so, then that's because -viewDidLoad is called before the view is actually presented. What you need to do is instead place your view logic in -viewDidAppear:. That will only be called when the view is finished animating and is completely visible.


You could move the code from viewDidLoad to viewDidAppear:.

0

精彩评论

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

关注公众号