views:

65

answers:

2

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?

A: 

There is some leak in AVAudioPlayer. You find the solution discussed in this forum itself.

Suresh Kumar
+1  A: 

First off: Make sure you're not trying to play a sound a second time that has already been released via [player release]; If you are, you'll get that error message right away. Once you release the player associated with a specific sound, you cannot play the file again like you have shown here. Try commenting out that line of code and see if it still happens.

I've also run into an issue where AVAudioPlayer allocates 32kb everytime a new player is created and if you have enough sounds you can run out of memory and get a crash. I highly doubt this is your problem though since it doesn't typically throw that error code.

EXC_BAD_ACCESS is typically from trying to access a pointer that no longer exists (such as your player object) for most of the cases I've seen on this forum

iWasRobbed
Thanks Rob! It turned out that I was freeing the view itself in some cases, so that caused the EXC_BAD_ACCESS since the sound was still playing but its delegate had been freed. I fixed it by adding a[self retain]immediately before calling [theAudio play], and then calling[self release]in audioPlayerDidFinishPlaying after [player release]. And that got rid of the crashes.
mindthief