androidiosaudiococos2d-xmarmalade

Problems to set volume of Cocos2d-X Simple Audio Engine?


I am developing a game using Cocos2d-X 2.2.6 on top of Marmalade SDK 7.8.0 and I am having some problems to set the sound effect volume of theCocos Denshion Simple Audio Engine.

My code is this:

void AudioHelper::init()
{
    s3eResult result;

    context = GameContext::getInstance();
    s3eSoundSetInt(S3E_SOUND_DEFAULT_FREQ, 44100);

    audioEngine = SimpleAudioEngine::sharedEngine();

    preloadSoundEffects();

    setVolume(context->getSoundVolume());
}

void AudioHelper::preloadSoundEffects()
{
    unsigned int i;
    CCArray *keysArray;
    CCString *key;
    const CCString *sound;

    keysArray = soundEffects->allKeys();

    for (i = 0; i < keysArray->count(); i++)
    {
        key = (CCString *) keysArray->objectAtIndex(i);
        sound = soundEffects->valueForKey(key->getCString());

        audioEngine->preloadEffect(sound->getCString());
    }
}

void AudioHelper::setVolume(int volumeLevel)
{
    float volume;

    // Note: there are 6 volume levels in my game, they go from 
    // 0 (max volume) to 5 (no sound).
    volume = 1 - volumeLevel / 5.0;

    audioEngine->setEffectsVolume(volume);
}

void AudioHelper::playSoundEffect(const char *effectKey)
{
    const CCString *sound;
    int volume;

    sound = soundEffects->valueForKey(effectKey);

    if (sound != NULL)
        audioEngine->playEffect(sound->getCString());

}

The problem is that the sound effects volume doesn't change when I call the setVolume method, except when I set the volume level parameter to 5 (no sound). When this happens no sounds are played (as expected).

What I am doing wrong?


Solution

  • did you log actual values in this code?

    void AudioHelper::setVolume(int volumeLevel)
    {
        float volume;
    
        // Note: there are 6 volume levels in my game, they go from 
        // 0 (max volume) to 5 (no sound).
        volume = 1 - volumeLevel / 5.0;
    
        audioEngine->setEffectsVolume(volume);
    }
    

    you cannot mix float and int calculations in such a way. float volume here would be 1 or 0 anyway, because you are using volumeLevel as type int. your code should look like this:

    void AudioHelper::setVolume(int volumeLevel)
    {
        float volume;
    
        // Note: there are 6 volume levels in my game, they go from 
        // 0 (max volume) to 5 (no sound).
        volume = 1.0f - (float)volumeLevel / 5.0f;
    
        audioEngine->setEffectsVolume(volume);
    }
    

    also take into account that there is some limitations using CocosDenshion at win platform (no volume changing there).