0

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.

MePo
  • 1,044
  • 2
  • 23
  • 48

2 Answers2

1
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);

 }
0

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.

MePo
  • 1,044
  • 2
  • 23
  • 48