androidcocos2d-xsimpleaudioengine

SimpleAudioEngine, background Volume down on playEffect then back up


In Cocos2d (android) Im trying to down the Background Volume when playEffect runs and than after the playEffect ends to bring it back up. I have this code:

auto audio = SimpleAudioEngine::getInstance();

audio->playBackgroundMusic("sound/abc-theme.mp3", true);

audio->setBackgroundMusicVolume(0.1);

audio->playEffect("sound/airplane.mp3", false, 1.0f, 1.0f, 1.0f);

audio->setBackgroundMusicVolume(1);

The problem is that i dont know how would i get playEffect end, this code "instantly" sets background music to 1. How would I put volume on Background music to 0.1 for the duration of PlayEffect.


Solution

  • I have done it like this

     const float Delay=1.0f;
     this->runAction(Sequence::create(DelayTime::create(Delay),CallFunc::create(CC_CALLBACK_0(ABC::bgVolumeUp, this)),nullptr));
    

    and function

    void ABC::bgVolumeUp(){
    
    audio->setBackgroundMusicVolume(1);
    
    }
    

    Basically its similar as pankaj suggested. He gave me the idea for the approach.