I am trying to get a sound to play in the iPhone Simulator (using 3.1 SDK). When I add a breakpoint, and step through the code in GDB, the sound plays. However, if I disable the breakpoint, the sound does not play.
The code to invoke the sound is:
SoundEffect *completeSound = [myobj completeSound];
if (completeSound != nil) {
[completeSound play];
}
The SoundEffect class has a simple play method:
// Plays the sound associated with a sound effect object.
-(void)play {
// Calls Core Audio to play the sound for the specified sound ID.
AudioServicesPlaySystemSound(_soundID);
}
If I enable a breakpoint above the "myobj completeSound" method, it plays perfectly. However, if there's no breakpoint, no sound is emitted.
The audio file is a CAF file that's 5 seconds in length.
Is there some delay that I need to add after invoking AudioServicesPlaySystemSound to ensure that it plays?
UPDATE: it appears that using the AudioServicesAddSystemSoundCompletion method is the solution.
SoundEffect *completeSound = [myobj completeSound];
if (completeSound != nil) {
AudioServicesAddSystemSoundCompletion([completeSound _soundID],
NULL, NULL, AudioPlaybackComplete, self);
[completeSound play];
}
Then the AudioPlaybackComplete static function is invoked when the audio clip is done. This works properly - although I still do not understand why the audio clip fails in the initial use case.