Getting notified when a sound is done playing in OpenAL

Mel picture Mel · Jun 25, 2009 · Viewed 8.4k times · Source

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.

Answer

pestilence669 picture pestilence669 · Dec 9, 2009

I didn't have much luck with callbacks in OpenAL. In my state machines, I simply poll the source and delay the transition until it's done.


    - (BOOL)playing {
        ALint sourceState;
        alGetSourcei(sourceID, AL_SOURCE_STATE, &sourceState);
        return sourceState == AL_PLAYING;
    }

// ... //

    case QSTATE_DYING:
        if (![audioSource playing])
            [self transitionTo:QSTATE_DEAD];

If this isn't what you need, then you're best bet is probably a timer. You shouldn't need to hardcode any values. You can determine the playback time when you're populating your buffers.

A bit of insight into the "why" of the question might offer some additional choices.