When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?
- (void)playOnce
You can probably do something like the following:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}