How to change volume with Simple Audio Engine

Hi,

I am developing a game for iOS and Android using Cocos2d-X 3.10 and I am having trouble chaning the effects volume using the Simple Audio Engine.

I’ve seen the documentation and the function that I am calling is: audioEngine->setEffectsVolume(volume);., however when I do this, nothing happens (note: I am testing my program on Windows using Visual Studio).

What is stranger is that when I went into the setEffectsVolume function, I found out that it has no implementation. From SimpleAudioEngine.cpp:

//////////////////////////////////////////////////////////////////////////
// volume interface
//////////////////////////////////////////////////////////////////////////

float SimpleAudioEngine::getBackgroundMusicVolume()
{
    return 1.0;
}

void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
{
}

float SimpleAudioEngine::getEffectsVolume()
{
    return 1.0;
}

void SimpleAudioEngine::setEffectsVolume(float volume)
{
}

Does this means that I can’t change my volume in Cocos2d-X? Would I have to implement these functions myself?

It works on Android (sets volume correctly), no effect on Windows (volume not changing).

Example code:

auto audio = CocosDenshion::SimpleAudioEngine::getInstance();
audio->setEffectsVolume(UserDefault::getInstance()->getFloatForKey("soundsVolume", 1.0f));
audio->setBackgroundMusicVolume(UserDefault::getInstance()->getFloatForKey("musicVolume", 1.0f));
2 Likes

Hi,

Yes, I’ve tested it on Android and it works!

Thanks for the help!

If you still need volume control on Windows, try new AudioEngine. It’s a little bit different, but still simple enough to use even by beginners. In cocos2d-x v3.10 it doesn’t support wav on Windows (again: works on Android), but works great with mp3 and ogg.

Header file is called AudioEngine.h, and it uses namespace “cocos2d::experimental” (it took me a while to find this detail).

There is no difference in background music and effects, so you have to store IDs returned by AudioEngine::play2d() function, or you can set the volume by calling play2d() with proper value. Example: experimental::AudioEngine::play2d(“sound.mp3”, false, 0.5f) plays sound.mp3, without looping (false), with volume set to 50% (0.5f).
Or you can use setVolume() after calling play2d(), with sound ID and the volume level. Example:
int id = experimental::AudioEngine::play2d(“sound.mp3”);
experimental::AudioEngine::setVolume(id, 0.5f);

And you can add “using namespace cocos2d::experimental;” to avoid typing “experimental” every time :wink:

3 Likes

Thank you so much!