0

In my application, I have buttons setup as a keyboard. Each buttons acts similar to a piano key, where you touch the button and the sound plays when you hold but stops when you release. For this method, I am using two IBActions. One for Touch Down (Sound Play) and Touch Up Inside (Sound Stop). I am using SystemSoundID for playing the sounds, as AVAudioPlayer has a delayed play. The only problem is, when I hold down the button the sound plays fine, but once I release the touch my app crashes. Below is my Code:

.h File:

SystemSoundID *soundID;

.m File:

- (IBAction)ASoundStart:(id)sender { 
     NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"P1:4t" ofType:@"mp3"];
     AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
     AudioServicesPlaySystemSound(soundID);
     [soundFile release];
 }

 - (IBAction)ASoundStop:(id)sender {
     AudioServicesDisposeSystemSoundID(soundID);
 }
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • I actually do not have ARC incorporated into my Project. Any way to stop the sound and prevent my App crashing? Why exactly _is_ my app crashing anyway? – Big Box Developer Jul 16 '12 at 03:58

1 Answers1

2

I believe you're treating SystemSoundID incorrectly as an object. It's simply an integer.

If you take a look at the declaration of AudioServicesPlaySystemSound, notice that it's argument is SystemSoundID and not SystemSoundID *

void AudioServicesPlaySystemSound (
   SystemSoundID inSystemSoundID
);

So if you just change SystemSoundID *soundID in your header to SystemSoundID soundID I believe it should work.

Happy coding.

Ncat
  • 417
  • 3
  • 10