Hi!
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:
-(void)fantKnapp{
NSLog(@"fant en knapp");
int ant =[self.antall intValue];
ant++;
NSLog(@"antall %i",ant);
self.antall=[NSNumber numberWithInt:ant];
self.poeng.text=[NSString stringWithFormat: @"%i/5 funnet",ant];
if(ant==5){
NSLog(@"Du klarte alle!");
klartAlleView.hidden=NO;
spillIgjen.hidden=NO;
nestebrettKnapp.hidden=NO;
navtestAppDelegate *appDelegate=(navtestAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate spillLyd:@"du_klarte_alle"];
}
else{
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...)