AudioEngine how to use it?

Hi…
What do audioID and AudioProfile parameters are for in cocos2d::experimental::AudioEngine and how to use them?

Thanks…

You can check it on the documentation:

https://docs.cocos2d-x.org/api-ref/cplusplus/v3x/d0/d75/classcocos2d_1_1experimental_1_1_audio_engine.html

but in most cases there is only needed the SimpleAudioEngine:

https://docs.cocos2d-x.org/api-ref/cplusplus/v3x/de/d8f/class_cocos_denshion_1_1_simple_audio_engine.html

thanks, but it would not explain in deep what they do and how to use…
but i have found how to point to specific bgm track for instance…

int intro_bgm = cocos2d::experimental::AudioEngine::play2d("intro.mp3", true, 1.0f);
cocos2d::experimental::AudioEngine::setVolume(intro_bgm, 0.5f);

that way has sense for me at least
thanks.

There it is explained, but yeah the documentation needs to be clearer in my point of view.

2 Likes

thanks… now that i have an idea, i´m wondering what is the better way to organize my music… if i identify them by an int (audioID), should store all them in a static std::vector of int maybe … how do you do?

1 Like

I define my Audio IDs in the header. This is adequate for my uses

2 Likes

There are many ways to store the audio ID, in this case I would use a std::map with a key/val pair, you can for this example set the key to the audio filename, and the value to the audio id:

ie: std::map<std::string,int> (Can also use better data types or enums for the key than just the filename)

Where a std::pair could be of the form <"audio1.mp3", [the audio id int value returned]>
Then you can search your map by the key.

A std::vector of audio IDs might not mean much to you if you need to know which ID relates to which audio source.

As @anon22094508 said, you can also store it as an instance variable to a singleton helper class (where I would recommend you keep the std::map if you go that route), or inside specific objects. This is good for sound effects that should be encapsulated with their objects. ie:
In the case of a spaceship that has a sound effect for the engine… you could have an instance variable on the spaceship class:

declare in header:
unsigned int audioIDEngine;

Source:
audioIDEngine = <returned value of sound effect>
now you can stop/modify the effect from the object itself.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.