Search code examples
iphoneaudiocore-audioopenal

Getting notified when a sound is done playing in OpenAL


I'm using OpenAL on iPhone to play multiple audio samples simultaneously.

Can I get OpenAL to notify me when a single sample is done playing?

I'd like to avoid hardcoding the sample length and setting a timer.


Solution

  • If you have the OpenAL source abstracted into a class, I guess you can simply call performSelector:afterDelay: when you start the sound:

    - (void) play
    {
        [delegate performSelector:@selector(soundHasFinishedPlaying)
            afterDelay:self.length];
        …
    }
    

    (If you stop the sound manually in the meantime, the callback can be cancelled, see the NSObject Class Reference.) Or you can poll the AL_SOURCE_STATE:

    - (void) checkStatus
    {
        ALint state;
        alGetSourcei(source, AL_SOURCE_STATE, &state);
        if (state == AL_PLAYING)
            return;
        [timer invalidate];
        [delegate soundHasFinishedPlaying];
    }
    

    I don’t know how to have OpenAL call you back. What exactly do you want the callback for? Some things can be solved better without a callback.