I am making an app for preschoolers with several mini-games. One of them involves finding "what is wrong" in the picture. For example if they press the kid playing in the street, a pling sounds, the child is moved out the street, and a label is updated to "1/5 errors found". If they find them all, they move on to a new viewcontroller with new traffic errors.

Everything is working just fine ...most of the time. But every once in a while the app plays the pling, and then freezes. I think it has to do with the actual playing of the sound (with AVAudioplayer), because if i comment that out, it seems to function everytime.

The function that is called when an error is found is this:


    NSLog(@"fant en knapp");
    int ant =[self.antall intValue];
    NSLog(@"antall %i",ant);
    self.antall=[NSNumber numberWithInt:ant];
    self.poeng.text=[NSString stringWithFormat: @"%i/5 funnet",ant];

        NSLog(@"Du klarte alle!");
        navtestAppDelegate *appDelegate=(navtestAppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate spillLyd:@"du_klarte_alle"];
        navtestAppDelegate *appDelegate=(navtestAppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate spillLyd:@"cling_1"];

This codes finds "antall", the current number of found errors (points). If all errors have been found a certain sound is played (this one always works). If not another sound plays. This is done by calling a function in the appdelegate file.

-(void)spillLyd:(NSString*) filnavn{
    NSString *audioFilePath=[[NSBundle mainBundle] pathForResource:filnavn ofType:@"aif"];
    NSURL *audioFileURL=[NSURL fileURLWithPath:audioFilePath];
    self.lydspiller=[[[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil] autorelease];
    [self.lydspiller prepareToPlay];
    [self.lydspiller play];
    NSLog(@"spiller lyden");

This works just fine in 49/50 cases, but every once in a while it doesnt. The funny thing is that the sound does play, but the app freezes, and the label is not updated (self.poeng.text=...). But all the NSLog comments are shown, including the new number of points. I would expect this to happen before the sound is played...

Is this an issue with the AVAudioplayer? Does anyone have similar experiences? Can anyone help me out on this one? I am thankful for any help or suggestions...

(And sorry about the norwegian. It probably doesnt make it any easier to help me...)

No correct solution


You probably need to implement the AVAudioPlayerDelegate methods so that you can try to trap errors from the player. If the delegate reports that the player finished successfully even though the app freezes, then the error is most likely not related to the player.

I feel for you. Few debugging task are more frustrating than tracking down a definite but highly intermittent bug.

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