Cocos2d-x 音频功能学习笔记 分类: cocos2d代码编写 2015-07-27 19:02 8人阅读 评论(0) 收藏
http://cn.cocos2d-x.org/tutorial/show?id=2448
游戏离不开声音!Cocos2d-x中提供了一个叫做SimpleAudioEngine的音频引擎。SimpleAudioEngine能够在游戏中播放背景音效以及游戏音效。SimpleAudioEngine是一个共享的单例对象,因此你可以在程序的任意地方调用它。就算是一个 HelloWorld 工程也可以很方便地使用这个引擎。SimpleAudioEgnine 支持多种格式的音频,比如MP3和CAF(Core Audio Forma)
新手入门
SimpleAudioEngine API的使用非常简单。
播放背景音乐
选择一个音频文件作为背景音乐,这个文件会被单曲循环一直播放。
|
1
2
3
4
5
|
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); |
播放音效
播放音效的方法如下:
|
1
2
3
4
|
auto
audio = SimpleAudioEngine::getInstance(); //
play a sound effect, just once. audio->playEffect("myEffect.mp3",false,
1.0f, 1.0f, 1.0f); |
暂停、停止、恢复音乐和音效的播放
当播放音乐和音效时,我们常常需要暂停、停止或者恢复它们。这些实现起来是比较简单的!
暂停
|
1
2
3
4
5
6
7
8
9
10
|
auto
audio = SimpleAudioEngine::getInstance();//
pause background music.audio->pauseBackgroundMusic();//
pause a sound effect.audio->pauseEffect();//
pause all sound effects.audio->pauseAllEffects(); |
停止
|
1
2
3
4
5
6
7
8
9
10
|
auto
audio = SimpleAudioEngine::getInstance();//
stop background music.audio->stopBackgroundMusic();//
stop a sound effect.audio->stopEffect();//
stops all running sound effects.audio->stopAllEffects(); |
Resume 恢复
|
1
2
3
4
5
6
7
8
9
10
|
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中,注意以下几个方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//
This function will be called when the app is inactive. When comes a phone call,//
it's be invoked toovoidAppDelegate::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 againvoidAppDelegate::applicationWillEnterForeground()
{ Director::getInstance()->startAnimation(); //
if you use SimpleAudioEngine, it must resume here //
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();} |
如果你要用SimpleAudioEngine实现背景音乐和音效,那么就需要注意别忘了去掉代码中有用代码的注释。
预加载音效
当游戏开始时,你需要预加载一些音效到内存中,以便当你想使用它们时能随时播放出来。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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"); |
音量
你可以通过程序的控制来增大减小音量。
|
1
2
3
4
|
auto
audio = SimpleAudioEngine::getInstance();//
setting the volume specifying value as a floataudio->setEffectsVolume(5.0f); |
PS.以上全文为Cocos引擎中文官网翻译校对,敬请勘误。转载请注明出自"Cocos引擎中文官网"。关于中间章节编写尚未全面完成,敬请期待。

浙公网安备 33010602011771号