Crashes when using AVAudioPlayer on iPhone

Posted by mindthief on Stack Overflow See other posts from Stack Overflow or by mindthief
Published on 2010-06-16T01:56:25Z Indexed on 2010/06/16 2:02 UTC
Read the original article Hit count: 396

Hi all, I am trying to use AVAudioPlayer to play some sounds in quick succession. When I invoke the sound-playing function less frequently so that the sounds play fully before the function is invoked again, the application runs fine. But if I invoke the function quickly in rapid succession (so that sounds are played while the previous sounds are still being played), the app eventually crashes after ~20 calls to the function, with the message "EXC_BAD_ACCESS". Here is code from the function:

NSString *nsWavPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:wavFileName];

AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:nsWavPath] error:NULL];
theAudio.delegate = self;

[theAudio play];

As mentioned in another thread, I implemented the following delegate function:

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if(!flag)
        NSLog(@"audio did NOT finish successfully\n");
    [player release];
}

But the app still crashes after around ~20 rapid calls to the function. Any idea what I'm doing wrong?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about avaudioplayer