Notification after running a Mix_Chunk


#1

Hi ! I’m toying with SDL_mixer, and I’m trying to execute some code only after my Mix_Chunk has finished playing.

Right now my quick & dirty code looks like

    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
        cerr << "SDL could not initialize: " << SDL_GetError() << endl;
        return -1;
    }

    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0) {
        cerr << "SDL_mixer could not initialize: " << Mix_GetError() << endl;
        SDL_Quit();
        return -1;
    }

    Mix_Chunk *gScratch = Mix_LoadWAV("high.wav");
    if (gScratch == nullptr) {
        cerr << "Failed to load scratch: " << Mix_GetError() << endl;
        /*....  free stuff and exit */
    }

    cout << "Starting sfx" << endl;
    int ret = Mix_PlayChannel(-1, gScratch, 4);
    if (ret < 0) {
        cerr << "Can't play channel: " << Mix_GetError() << endl;
    } else {
        cout << "Playing on channel: " << ret << endl;
        while (Mix_Playing(ret) == 1) {} /* actively waiting for the end */
        cout << "sfx done" << endl;
    }

and I get the correct behaviour, but my system is “blocked” during the execution of the chunk.
It’s a short one so that could be “ok”, but I wonder if there’s a way to continue running some code, and be notified (maybe by an SDL_event ?) when the channel goes from Playing to Pause.
Does such a thing exists ?