I’m trying to create a Soundboard app, but I’m having difficulty finding a way to play sounds and making the sound stop when the other sound button is pressed. I want to prevent the sound from overlapping each other. And each sound will loop. Is there a way to prevent more than one sound playing at the same time? I have not found a way in any tutorials. The code I’m using for the soundboard looks a little like this:
-(IBAction)sound1 {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound1", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
-(IBAction)sound2 {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sound2", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
You should use AVAudioPlayer so you can play or stop the sound at any time, and use delegate methods to determine if the sound was interrupted or has finished.