2

I was reading this post on stack overflow about using AudioServicesPlaySystemSound to play one sound at a time.

stop sound in iPhone

I had the same problem described, with the following code, when you press one button before the other stops playing, the 2nd sound is played on top of the first one so that the 2 sounds overlap.

-(IBAction)button1:(id)sender{
SystemSoundID soundID;   
NSString *soundFile = [[NSBundle mainBundle]    
                       pathForResource:@"sound1"
                       ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile],   &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
} 

-(IBAction)button2:(id)sender{
SystemSoundID soundID;   
NSString *soundFile = [[NSBundle mainBundle]    
                       pathForResource:@"sound2"
                       ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}

According to apple's documentation:

AudioServicesPlaySystemSound is supposed to only play one sound at a time.

In addition, when you use the AudioServicesPlaySystemSound function:

  • Sounds play at the current system audio volume, with no programmatic volume control available
  • Sounds play immediately
  • Looping and stereo positioning are unavailable
  • Simultaneous playback is unavailable: You can play only one sound at a time

So I don't understand why more than one sound can play at the same time.

I tried using AudioServicesDisposeSystemSoundID, but it the sounds still overlaps, am I doing it wrong?

-(IBAction)sound2:(id)sender{
AudioServicesDisposeSystemSoundID
SystemSoundID soundID;   
NSString *soundFile = [[NSBundle mainBundle]    
                       pathForResource:@"sound2"
                       ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}

Currently, I am using AVAudioPlayer instead but there is a sound start delay, and I would like to use AudioServicesPlaySystemSound, if possible, as it plays sound immediately.

Has anyone else had the same problem, please?

Thank you very much for your time.

1 Answer 1

4

Try with AudioServicesDisposeSystemSoundID.

It's just a solution to solve your problem. You have to create the sound again if you need to play it again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.