# Set up a callback when sound end

**URL:** <https://qa.fmod.com/t/set-up-a-callback-when-sound-end/12664>\
**Category:** FMOD Engine\
**Created:** [August 25, 2016, 1:56pm UTC](https://qa.fmod.com/t/set-up-a-callback-when-sound-end/12664 "2016-08-25T13:56:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrancescoLasa](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@FrancescoLasa](https://qa.fmod.com/u/FrancescoLasa)\
**Post date:** [August 25, 2016, 1:56pm UTC](https://qa.fmod.com/t/set-up-a-callback-when-sound-end/12664/1 "2016-08-25T13:56:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nick1](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@nick1](https://qa.fmod.com/u/nick1)\
**Post date:** [August 26, 2016, 12:50am UTC](https://qa.fmod.com/t/set-up-a-callback-when-sound-end/12664/2 "2016-08-26T00:50:36Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![FrancescoLasa](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@FrancescoLasa](https://qa.fmod.com/u/FrancescoLasa)\
**Post date:** [August 26, 2016, 8:54am UTC](https://qa.fmod.com/t/set-up-a-callback-when-sound-end/12664/3 "2016-08-26T08:54:36Z")

</div>

> [@](#):
>
> 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);
> 
> ```

Thank you very much!
