SimpleAudioEngine, background Volume down on playEffect then back up

306 Views Asked by At

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.

2

There are 2 best solutions below

0
MePo On BEST ANSWER

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.

0
pankaj khanduja - Game Changer On
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);

this->scheduleOnce(schedule_selector(LoadingScene::loadingCallBack), 1.0f);

create a function

void ClassName::loadingCallBack(){

 audio->setBackgroundMusicVolume(1);

 }