一款开发精良的游戏离不开精心制作的声音效果。Cocos2d-x中提供了一个叫做SimpleAudioEngine的音频引擎。SimpleAudioEngine能够在游戏中播放背景音效以及游戏音效。SimpleAudioEngine是一个共享的单例对象,因此你可以在程序的任意地方调用它。就算是一个 HelloWorld 工程也可以很方便地使用这个引擎。SimpleAudioEgnine 支持多种格式的音频,比如MP3和CAF(Core Audio Forma)
新手入门
SimpleAudioEngine API的使用非常简单。
播放背景音乐
选择一个音频文件作为背景音乐,这个文件会被单曲循环一直播放。
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);
播放音效
播放音效的方法如下:
auto audio = SimpleAudioEngine::getInstance();
// play a sound effect, just once.
audio->playEffect("myEffect.mp3", false, 1.0f, 1.0f, 1.0f);
暂停、停止、恢复音乐和音效的播放
当播放音乐和音效时,我们常常需要暂停、停止或者恢复它们。这些实现起来是比较简单的!
暂停
auto audio = SimpleAudioEngine::getInstance();
// pause background music.
audio->pauseBackgroundMusic();
// pause a sound effect.
audio->pauseEffect();
// pause all sound effects.
audio->pauseAllEffects();
停止
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();
音频高级功能
Setup 设置
SimpleAudioEngine的API非常简单,但是在游戏中使用还是有一些注意事项,尤其是在手机和平板的等移动设备中使用时。比如在多个APP中切换时应如何处理,在或者当你玩着游戏时有电话打进来又该怎么办?这些异常在制作游戏时都必须提前想好处理方法,当然幸运的是,你能想到的异常引擎都帮我们做好了,你只需使用就好。
在AppDelegate.cpp中,注意以下几个方法:
// 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();
}
如果你要用SimpleAudioEngine实现背景音乐和音效,那么就需要注意别忘了去掉代码中有用代码的注释。
预加载音效
当游戏开始时,你需要预加载一些音效到内存中,以便当你想使用它们时能随时播放出来。
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");
音量
你可以通过程序的控制来增大减小音量。
auto audio = SimpleAudioEngine::getInstance();
// setting the volume specifying value as a float
audio->setEffectsVolume(5.0f);