I have a sound file that I use throughout my program:
NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"mp3"];
NSURL *soundFile1 = [[NSURL alloc] initFileURLWithPath:soundPath1];
soundFilePlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile1 error:nil];
[soundFilePlayer1 prepareToPlay];
So soundFilePlayer1 is defined in my .h file because I need it to be global.
Now… do I need to release soundFilePlayer1 and soundFile1? (and soundPath1?)
If so… where do I do this?
I think I need to release soundFilePlayer1 in: (but do I need to stop it first since it may be playing when they exit the program?)
- (void)dealloc {
[soundFilePlayer1 release];
}
what about the other files… where do I release those?
Thanks
Remember a simple rule of thumb. Only release if you have called
alloc,init, ornewon the object. So, you’ll want to releasesoundFile1. Alternatively, you can use a convenience method and have the sound file added to the autorelease pool automatically:And yes, you can release
player(soundFilePlayer1) indealloc, but if you’re not looping the audio file there’s a better way. Conform to theAVAudioPlayerDelegate:Then implement the method here: