This is a long standing issue for me, which I’ve been unable to solve.
I’m trying to play a sequence of small audio files. However I get a memory leak if I don’t release the AVAudioPlayer, if I do release it the file doesn’t play at all.
I’ve tried autorelease, but nothing works.
- (void) playSoundShowLabel:(NSTimer*)theTimer {
NSMutableDictionary *dict = [[[NSMutableDictionary alloc]
initWithDictionary:[theTimer userInfo]] autorelease];
NSURL *clickURL = [NSURL fileURLWithPath:[NSString
stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],
[dict valueForKey:@"sWav"]]];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:
clickURL error:nil];
[theAudio play];
theAudio.numberOfLoops = 0;
//[theAudio release]; << stops play
}
I have tried other ways to play files with system sound but I get other problems with this, so I do need to use AVAudioPlayer.
I have googled this but not found an answer 🙁
Use an instance variable for the player and make sure it stays valid until the audio is done or should be aborted.
For the former, use its delegate – for the latter, use viewDidUnload or dealloc.
For now, I am assuming that you are not using ARC…
Within your interface declaration:
Within your class implementation: