views:

201

answers:

1

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.

+2  A: 

Have you checked your program flow? Because it leads me to believe that without the breakpoint your program repeatedly fires the play message and restarts the playback of the sound many times with so high frequency you never get to hear any samples of it. With the breakpoint, you halt your code from disrupting the playback.

Cecil Has a Name
No, this is not the issue. There's only 1 audio clip. And it plays one time, then there is a several minute pause waiting for user input.
Dave Viner
Okay, my bad then. (But are you sure? Have you put a log statement in it, or a thread sleep message just after you send the `play` message?)
Cecil Has a Name