Set up a callback when sound end

Hi, I use FMOD low level api 5 with Microsoft Visual Studio 2013.
I create some class to do a simple audio manager. Now I’d create a system to reproduce continuosly background music, so when a music end, the class automatically start an other.
So I implemented a class with this methods following the FMOD documentation:

FMOD_RESULT F_CALLBACK MusicMachine::EndOfSong(FMOD_CHANNELCONTROL*channelControl, FMOD_CHANNELCONTROL_TYPE controlType,
	FMOD_CHANNELCONTROL_CALLBACK_TYPE callbackType, void*commanData1, void*commanData2)
{
	if (controlType == FMOD_CHANNELCONTROL_CHANNEL&&callbackType==FMOD_CHANNELCONTROL_CALLBACK_END){	
		//se è un singolo channel è non un channel group e se la callback è chiamata quanto è finita la riproduzione del suono

		FMOD::Channel*channel = (FMOD::Channel*)channelControl;

		std::cout << "Chiamata n: " << ++currentCall << std::endl;
	}


	return FMOD_OK;
}

Then to set up correctly this function to call when a song finished i add this code before the game loop of my game
    FMOD::System_Create(&m_system);
    m_system->init(100, FMOD_INIT_NORMAL, 0);
    FMOD::Sound*sound = nullptr;
    FMOD_RESULT error = m_system->createSound("song", m_modes[type], 0, &sound);
    FMOD::Channel*m_song=nullptr;
    m_system->playSound(sound, 0, false, &m_song);
    MusicMachine*m_musicMachine* = new MusicMachine();
    
    //set up the callback
    m_song>setCallback((FMOD_CHANNELCONTROL_CALLBACK)m_musicMachine>EndOfSong((FMOD_CHANNELCONTROL*)m_song,FMOD_CHANNELCONTROL_CHANNEL, FMOD_CHANNELCONTROL_CALLBACK_END, 0, 0));   

The mistake is that the function was called immidiately when I use the methods
m_song>setCallback((FMOD_CHANNELCONTROL_CALLBACK)m_musicMachine>EndOfSong((FMOD_CHANNELCONTROL*)m_song,FMOD_CHANNELCONTROL_CHANNEL, FMOD_CHANNELCONTROL_CALLBACK_END, 0, 0));

and not when the song end! I also implement the gameloop and each frame (60 FPS) I called
m_system->update();

I don't know what was wrong, I only need that the methods "EndOfSong" is called when a sound end.. Thank you for help!

This is just a C++ coding issue.

First your EndOfSong function needs to be marked static.

Second you need to pass the function to setCallback. At the moment your code is calling the function. You should never need to cast to FMOD_CHANNELCONTROL_CALLBACK, forcing the cast hides errors that can hit you later (including crashes).

m_song->setCallback(MusicMachine::EndOfSong);
1 Like

Thank you very much!