Cocos2d-x Developer Guide 11: Audio

 A well-developed game cannot do without elaborate sound effects. Cocos2d-x provides an audio engine called SimpleAudioEngine. SimpleAudioEngine can play background sound effects and game sound effects in the game. SimpleAudioEngine is a shared singleton object, so you can call it anywhere in the program. Even a HelloWorld project can easily use this engine. SimpleAudioEgnine supports multiple audio formats, such as MP3 and CAF (Core Audio Forma)

Beginner's guide

     The SimpleAudioEngine API is very simple to use.

Play background music

     Choose an audio file as the background music, this file will be played continuously by a single loop.

auto audio = SimpleAudioEngine::getInstance();

// set the background music and continuously play it.

audio->playBackgroundMusic("mymusic.mp3", true);

// set the background music and play it just once.

audio->playBackgroundMusic("mymusic.mp3", false);

Play sound

       The method of playing sound effects is as follows:

auto audio = SimpleAudioEngine::getInstance();

// play a sound effect, just once.

audio->playEffect("myEffect.mp3", false, 1.0f, 1.0f, 1.0f);

Pause, stop, and resume music and sound effects

       When playing music and sound effects, we often need to pause, stop or resume them. These are relatively simple to implement!

time out

auto audio = SimpleAudioEngine::getInstance();

// pause background music.

audio->pauseBackgroundMusic();

// pause a sound effect.

audio->pauseEffect();

// pause all sound effects.

audio-> pauseAllEffects ();

stop

auto audio = SimpleAudioEngine::getInstance();

// stop background music.

audio->stopBackgroundMusic();

// stop a sound effect.

audio->stopEffect();

// stops all running sound effects.

audio->stopAllEffects();

Resume

auto audio = SimpleAudioEngine::getInstance();

// resume background music.

audio->resumeBackgroundMusic();

// resume a sound effect.

audio->resumeEffect();

// resume all sound effects.

audio->resumeAllEffects();

Audio advanced features

Setup

The API of SimpleAudioEngine is very simple, but there are still some precautions when using it in games, especially when using it in mobile devices such as mobile phones and tablets. For example, how to deal with switching between multiple apps, or when a phone call comes in while you are playing a game? These exceptions must be handled in advance when making a game. Of course, luckily, you can The exception engines that we think of are all done for us, you just need to use it.

In AppDelegate.cpp, pay attention to the following methods:

// This function will be called when the app is inactive. When comes a phone call,

// it's be invoked too

void AppDelegate::applicationDidEnterBackground() {

Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause

// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();

}

// this function will be called when the app is active again

void AppDelegate::applicationWillEnterForeground() {

Director::getInstance()->startAnimation();

// if you use SimpleAudioEngine, it must resume here

// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();

}

       If you want to use SimpleAudioEngine to achieve background music and sound effects, then you need to be careful not to forget to remove the comments of useful codes in the code.

Preloaded sound effects

        When the game starts, you need to pre-load some sound effects into the memory so that they can be played whenever you want to use them.

auto audio = SimpleAudioEngine::getInstance();

// pre-loading background music and effects. You could pre-load

// effects, perhaps on app startup so they are already loaded

// when you want to use them.

audio->preloadBackgroundMusic("myMusic1.mp3");

audio->preloadBackgroundMusic("myMusic2.mp3");

audio->preloadEffect("myEffect1.mp3");

audio->preloadEffect("myEffect2.mp3");

// unload a sound from cache. If you are finished with a sound and

// you wont use it anymore in your game. unload it to free up

// resources.

audio->unloadEffect("myEffect1.mp3");

volume

      You can increase or decrease the volume through the control of the program.

auto audio = SimpleAudioEngine::getInstance();

// setting the volume specifying value as a float

audio->setEffectsVolume(5.0f);

Guess you like

Origin blog.csdn.net/qq_21743659/article/details/108660156