SimpleAudioEngine need unloadBackgroundMusic()

Hi!

In my game there are background music and effects. By default, music and effects are enabled. I want to allow the user to turns off the background music or all sounds.

If the user turns off all sounds, then I use something like

audio->end();

It should free all audio resources. Yes? Will it destroy my audio Instance?

If the user turn off only the background music, then I NEED use something like

 audio->unloadBackgroundMusic();

I do not want to use audio->end() because I still needed effects.

There is only a small problem - There is no unloadBackgroundMusic() function.

I use the SimpleAudioEngine. I read that there is NewAudioEngine, but it has problems on the iOS sometimes.

Any ideas?

So, or my question is too complex, or it is incorrect.

I played with the code, and it seems that I can use audio->end() and then use audio->preloadEffect() again. I’m not sure this is the right and safe way.

Why not just add unloadBackgroundMusic() to SimpleAudioEngine? Is my case too exclusive? Or is there an easier way?

@slackmoehrle Can you help please?

I think when you play music it’s playing like a stream so it’s not entirely in memory. stopBackgroundMusic would be enough. Also on android preloadEffect doesn’t do anything.

I, instead, have a wrapper functions for using sounds, where I choose from file extension (depending on platform) and have flags checking if music or sounds is enabled before playing anything. So when I click a button, which disable music I simply call stopBackgroundMusic and remember setting (using UserDefault) that music is off. As for sounds you just can call stopAllEffects() and again store information that sounds is off.

1 Like

Hi @piotrros !

You are right implementation of the preloadBackgroundMusic is really empty. I should have see it before. I am bad.

I also store the settings in a file or read it while running the game. I just wanted to free up resources in the current game session if the user decides that he does not need music. Although it seems that it is unnecessary perfectionism.

So if the preloadBackgroundMusic does nothing, then it seems unloadBackgroundMusic is not needed.

Thank you very much! You really helped me.