Play an audio twice or more at different time

Play an audio twice or more at different time

Hi there!

if I understand well, i save into an int type an instance of an audio when i play it:

int bgm_main = cocos2d::AudioEngine::play2d("./bgm.ogg", true);

so i can set some properties like:

cocos2d::AudioEngine::setVolume(bgm_main, 0.7f);

but what is the way to play the same audio again?.. do i need to redo this?:

int bgm_main = cocos2d::AudioEngine::play2d("./bgm.ogg", true);

or can i usue bgm_main in other way?


Depending on the scope, you could re-use. I can’t tell you exactly because I don’t know where you have this defined and where else you plan to use it…I.e another class and how these classes relate together.

The audio files are cached, btw.

i have created an abstract superclass called scnMaster, it has an implementation to play an audio that returns an integer. Typically this:

inline int bgm_play(const char* _file_name, bool _loop, const float _vol){
     return cocos2d::AudioEngine::play2d(_file_name, _loop, _vol);

My scenes inherit from scnMaster so when i want to play a bgm, i cache it like this:

int bgmID_mainmenu = 0;
void start_scene();


void scnMyScene::start_scene(){
     bgmID_mainmenu = bgm_play("./mainbgm.ogg", true, 1.0f);
     //do other stuffs
     //do other stuffs
     //want to play again bgmID_mainmenu... do i need another ID request?