|   版权声明:本文为博主原创文章,未经博主允许不得转载。

音效简介:

1.1 在游戏开发的过程中除了华丽的界面,生动的动画之外,适当的音效也是重要的一部分

1.2 游戏中的声音分为两类,一类是音乐,播放时间较长,适合用作背景音乐。另一类是音效,播放时间较短,可以重复的播放,例如,子弹打出的声音,地鼠触动的声音。             
1.3 cocos2d-x集成了CocosDenshion音效引擎,可以方便的实现游戏音效。CocosDenshion提供了多个音效API,它们分别是CDSoundEngine,CDAudioManager和SimpleAudioEngine。前两个是比较底层的可以控制多个声音的播放,在游戏开发中我们一般使用SimpleAudioEngine就足够了。

1.4 使用SimpleAudioEngine比较的简单,只要添加头文件和命名空间就可以使用了。#include
"SimpleAudioEngine.h";即可使用。

1.5
在Cocos2d-x中不同的平台之下,所支持的音乐格式有些差异:

Cocos2d-x在不同平台下支持的背景音乐格式:

Android:  MP3,
WAV, 3GP

IOS: MP3, CAF

Win32:  MID,WAV

Cocos2d-x在不同平台下支持的音效音乐格式:

Android:   OGG, WAV

IOS: CAF

Win32:  MID,WAV

音乐不同平台的判别:

音效文件

  1. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  2.  
  3. #define EFFECT_FILE "effect.ogg"
  4.  
  5. #elif (CC_TARGET_PLATFORM == CC_PLATFROM_MARMALADE)
  6.  
  7. #define EFFECT_FILE "effect.raw"
  8.  
  9. #else
  10.  
  11. #define EFFECT_FILE "effect.wav"
  12.  
  13. #endif // CC_PLATFORM_ANDROID

背景音乐文件

  1. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  2.  
  3. #define MUSIC_FILE "music.mid"
  4.  
  5. #elif (CC_TARGET_PLATFROM == CC_PLATFORM_BLACKBERRY)
  6.  
  7. #define MUSIC_FILE "music.ogg"
  8.  
  9. #else
  10.  
  11. #define MUSIC_FILE "music.mp3"
  12.  
  13. #endif // CC_PLATFORM_WIN32

音效API中的重要函数:

  1. /** Returns a shared instance of the SimpleAudioEngine.*/
  2. 实例化
  3. static SimpleAudioEngine* getInstance();
  4. /** Release the shared Engine object.warning It must be called before the application exit, or it will lead to memory leaks.*/
  5. static void end();
  6. /** Preload background music.param filePath The path of the background music file.*/
  7. 预处理背景音乐问ujianjiang压缩格式的文件进行解压处理,如MP3解压为WAV
  8. virtual void preloadBackgroundMusic(const char* filePath);
  9. /** Play background music.param filePath The path of the background music file,or the FileName of T_SoundResInfo.param loop Whether the background music loop or not.*/
  10. 播放背景音乐,参数bLoop控制是否循环播放,默认为false
  11. virtual void playBackgroundMusic(const char* filePath, bool loop = false);
  12. /** Stop playing background music.param releaseData If release the background music data or not.As default value is false.*/
  13. 停止播放音乐
  14. virtual void stopBackgroundMusic(bool releaseData = false);
  15. /** Pause playing background music.*/
  16. 暂停播放背景音乐
  17. virtual void pauseBackgroundMusic();
  18. /** Resume playing background music.*/
  19. 继续播放背景音乐
  20. virtual void resumeBackgroundMusic();
  21. /** Rewind playing background music.*/
  22. 倒带
  23. virtual void rewindBackgroundMusic();
  24. /** Indicates whether any background music can be played or not.return <i>true</i> if background music can be played, otherwise <i>false</i>.*/
  25. virtual bool willPlayBackgroundMusic();
  26. /** Indicates whether the background music is playing.return <i>true</i> if the background music is playing, otherwise <i>false</i>.*/
  27. 判断背景音乐是否在播放
  28. virtual bool isBackgroundMusicPlaying();
  29. /** The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.*/
  30. virtual float getBackgroundMusicVolume();
  31. /** Set the volume of background music.param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.*/
  32. 设置背景音乐的音量大小
  33. virtual void setBackgroundMusicVolume(float volume);
  34. /** The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.*/
  35. 获得背景音乐的音量大小
  36. virtual float getEffectsVolume();
  37. /** Set the volume of sound effects.param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.*/
  38. virtual void setEffectsVolume(float volume);
  39. /** Play sound effect with a file path, pitch, pan and gain.
  40. * @param filePath The path of the effect file.
  41. * @param loop Determines whether to loop the effect playing or not. The default value is false.
  42. * @param pitch Frequency, normal value is 1.0. Will also change effect play time.
  43. * @param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
  44. * @param gain Volume, in the range of [0..1]. The normal value is 1.
  45. * @return The sound id.
  46. *
  47. * @note Full support is under development, now there are limitations:
  48. * - no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
  49. * - no pitch/pan/gain on win32.
  50. */
  51. 播放音频
  52. virtual unsigned int playEffect(const char* filePath, bool loop = false,float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);
  53. /** Pause playing sound effect.param soundId The return value of function playEffect.*/
  54. 暂停播放所有音效,参数SoundIdplayEffect函数返回ID
  55. virtual void pauseEffect(unsigned int soundId);
  56. /** Pause all playing sound effect.*/
  57. 暂停播放所有音效
  58. virtual void pauseAllEffects();
  59. /** Resume playing sound effect.param soundId The return value of function playEffect.*/
  60. 继续播放音效,参数SoundIdplayEffect函数返回ID
  61. virtual void resumeEffect(unsigned int soundId);
  62. /** Resume all playing sound effect.*/
  63. 继续播放所有音效
  64. virtual void resumeAllEffects();
  65. /** Stop playing sound effect.param soundId The return value of function playEffect.*/
  66. 停止播放所有音效,参数soundIdplayEffect函数返回ID
  67. virtual void stopEffect(unsigned int soundId);
  68. /** Stop all playing sound effects.*/
  69. 停止播放所有音效
  70. virtual void stopAllEffects();
  71. /** Preload a compressed audio file.The compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine.param filePath The path of the effect file.*/
  72. 预处理音频文件,将压缩格式的文件进行解压处理,如MP3解压为WAV
  73. virtual void preloadEffect(const char* filePath);
  74. /** Unload the preloaded effect from internal buffer.param filePath The path of the effect file.*/
  75. virtual void unloadEffect(const char* filePath);

实例:

  1. .h files
  2.  
  3. #ifndef _SOUNDTEST_SCENE_H_
  4. #define _SOUNDTEST_SCENE_H_
  5. //使用音效比较的简单我们只要包含托文件SimpleAudioEngine.h和命名空间CocosDenshion
  6. #include "cocos2d.h"
  7. #include "ui\CocosGUI.h"
  8. #include "editor-support/cocostudio/CCSGUIReader.h"
  9. #include "SimpleAudioEngine.h"
  10.  
  11. //设置音乐格式的宏
  12. #define BG_FILES "MainMenuMusic1.wav"
  13. #define EFFECT_FILES "mole.wav"
  14. USING_NS_CC;
  15. using namespace cocos2d::ui;
  16. using namespace CocosDenshion;
  17. class soundTest : public cocos2d::Layer
  18. {
  19. private:
  20. public:
  21. static cocos2d::Scene* createScene();
  22. virtual bool init();
  23. //Player music
  24. void testPlayBGMusic(Ref* snedef);
  25. void testPlayEffMusic(Ref* snedef);
  26. //Pause music
  27. void testPauseBGMusic(Ref* snedef);
  28. void testPauseEffMusic(Ref* snedef);
  29. //Resume music
  30. void testResumeBGMusic(Ref* snedef);
  31. void testResumeEffMusic(Ref* snedef);
  32. //Stop music
  33. void testStopBGMusic(Ref* snedef);
  34. void testStopEffMusic(Ref* snedef);
  35. //CallBack function
  36. void sliderEvent1(Ref* sendef, SliderEventType type);
  37. void sliderEvent2(Ref* sendef, SliderEventType type);
  38. CREATE_FUNC(soundTest);
  39. };
  40. #endif // _SOUNDTEST_SCENE_H_
  41.  
  42. .cpp files
  43.  
  44. #include "SoundTest.h"
  45.  
  46. Scene* soundTest::createScene()
  47. {
  48. auto scene = Scene::create();
  49. auto layer = soundTest::create();
  50. scene->addChild(layer);
  51. return scene;
  52. }
  53. bool soundTest::init()
  54. {
  55. if (!Layer::init())
  56. {
  57. return false;
  58. }
  59.  
  60. Size size = Director::getInstance()->getWinSize();
  61. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  62.  
  63. SimpleAudioEngine::getInstance()->preloadBackgroundMusic(BG_FILES);
  64. SimpleAudioEngine::getInstance()->preloadEffect(EFFECT_FILES);
  65.  
  66. SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.2);
  67. SimpleAudioEngine::getInstance()->setEffectsVolume(0.2);
  68.  
  69. auto item1 = MenuItemFont::create("Play bg", CC_CALLBACK_1(soundTest::testPlayBGMusic, this));
  70. auto item2 = MenuItemFont::create("Play eff", CC_CALLBACK_1(soundTest::testPlayEffMusic, this));
  71.  
  72. auto item3 = MenuItemFont::create("Pause bg", CC_CALLBACK_1(soundTest::testPauseBGMusic, this));
  73. auto item4 = MenuItemFont::create("Pause ef", CC_CALLBACK_1(soundTest::testPauseEffMusic, this));
  74.  
  75. auto item5 = MenuItemFont::create("Resume bg", CC_CALLBACK_1(soundTest::testResumeBGMusic, this));
  76. auto item6 = MenuItemFont::create("Resume eff", CC_CALLBACK_1(soundTest::testResumeEffMusic, this));
  77.  
  78. auto item7 = MenuItemFont::create("Stop bg", CC_CALLBACK_1(soundTest::testStopBGMusic, this));
  79. auto item8 = MenuItemFont::create("Stop eff", CC_CALLBACK_1(soundTest::testStopEffMusic, this));
  80.  
  81. auto menu1 = Menu::create(item1, item3, item5, item7, NULL);
  82. menu1->setPosition(Vec2(100, size.height - 100));
  83. menu1->alignItemsVertically();
  84. this->addChild(menu1);
  85.  
  86. auto menu2 = Menu::create(item2, item4, item6, item8, NULL);
  87. menu2->setPosition(Vec2(300, size.height - 100));
  88. menu2->alignItemsVertically();
  89. this->addChild(menu2);
  90.  
  91. Slider* slider1 = Slider::create();
  92.  
  93. slider1->loadBarTexture("sliderTrack.png");
  94. slider1->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", "");
  95. slider1->loadProgressBarTexture("sliderProgress.png");
  96. slider1->setPosition(Vec2(200, 300));
  97. slider1->addEventListenerSlider(this, sliderpercentchangedselector(soundTest::sliderEvent1));
  98. this->addChild(slider1);
  99.  
  100. Slider* slider2 = Slider::create();
  101.  
  102. slider2->loadBarTexture("sliderTrack.png");
  103. slider2->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", "");
  104. slider2->loadProgressBarTexture("sliderProgress.png");
  105. slider2->setPosition(Vec2(size.width - 200, 10));
  106. slider2->addEventListenerSlider(this, sliderpercentchangedselector(soundTest::sliderEvent1));
  107. this->addChild(slider2);
  108.  
  109. return true;
  110. }
  111. //设置调节音量大小条的回调函数
  112. void soundTest::sliderEvent1(Ref* sendef, SliderEventType type)
  113. {
  114. if (type == SLIDER_PERCENTCHANGED)
  115. {
  116. Slider* slider = dynamic_cast<Slider*>(sendef);
  117. int percent = slider->getPercent();
  118. SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(percent*0.1);
  119. }
  120. }
  121. void soundTest::sliderEvent2(Ref* sendef, SliderEventType type)
  122. {
  123. if (type == SLIDER_PERCENTCHANGED)
  124. {
  125. Slider* slider = dynamic_cast<Slider*>(sendef);
  126. int percent = slider->getPercent();
  127. SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(percent*0.1);
  128. }
  129. }
  130. //开始播放音乐
  131. void soundTest::testPlayBGMusic(Ref* sendef)
  132. {
  133. SimpleAudioEngine::getInstance()->playBackgroundMusic(BG_FILES, true);
  134. }
  135. //开始播放音效
  136. void soundTest::testPlayEffMusic(Ref* sendef)
  137. {
  138. SimpleAudioEngine::getInstance()->playEffect(EFFECT_FILES);
  139. }
  140. //暂停播放的音乐
  141. void soundTest::testPauseBGMusic(Ref* sendef)
  142. {
  143. SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
  144. }
  145. //暂停播放所有的音效
  146. void soundTest::testPauseEffMusic(Ref* sendef)
  147. {
  148. SimpleAudioEngine::getInstance()->pauseAllEffects();
  149. }
  150. //恢复播放的音乐
  151. void soundTest::testResumeBGMusic(Ref* sendef)
  152. {
  153. SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
  154. }
  155. //恢复播放所有的音效
  156. void soundTest::testResumeEffMusic(Ref* sendef)
  157. {
  158. SimpleAudioEngine::getInstance()->resumeAllEffects();
  159. }
  160. //停止播放的音乐
  161. void soundTest::testStopBGMusic(Ref* sendef)
  162. {
  163. SimpleAudioEngine::getInstance()->stopBackgroundMusic();
  164. }
  165. //停止播放所有的音效
  166. void soundTest::testStopEffMusic(Ref* sendef)
  167. {
  168. SimpleAudioEngine::getInstance()->stopAllEffects();
  169. }

Cocos2d-x之Sound的更多相关文章

  1. 【Cocos2d入门教程八】浅析Cocoss2d下的音频引擎及封装音频类

    Cocos2d-x提供了一个音频CocosDenshion引擎,CocosDenshion引擎可以独立于Cocos2d-x单独使用,CocosDenshion引擎本质上封装了OpenAL音频处理库.具 ...

  2. Cocos2D iOS之旅:如何写一个敲地鼠游戏(十一):完善游戏逻辑

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  3. Cocos2D:塔防游戏制作之旅(十八)

    在Enemy.m的getDamaged:方法只给你添加如下1行(在if条件内): [theGame awardGold:200]; 现在运行游戏你将注意到你不能放置超出你资源金币的炮塔了.当然杀死敌人 ...

  4. 基于cocos2d开发的android小游戏——採花仙

    /*cocos 2d 已经成为了如今移动端游戏开发的强有力的工具,眼下主流游戏中多採用cocos 2d游戏引擎. 我也尝试了一下该引擎.我是用的是cocos2d-android,以后要移植到Cocos ...

  5. java sound初探

    网上关于java sound的正规资源讲解的非常好,本文不再给出示例,主要提供一些好的资源,并说说我的一些理解,用于形成对java sound的整体认识. 一.几个词汇 TTS:text-to-spe ...

  6. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  7. TeamViewer 12.0.71503 Patch By.Sound

    TeamViewer - the All-In-One Software for Remote Support and Online Meetings - Remote control any com ...

  8. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

  9. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

随机推荐

  1. jenkin 构建失败 才发邮件通知

    使用场景:自动化测试,一般需要配置定时执行(每天执行一次,没周执行一次),如果有失败,则发邮件给相关人员关注.此时需要使用jenkins的邮件发送配置.修改job的configure配置步骤如下: 1 ...

  2. jar包/class文件如何快速反编译成java文件

    有时编写的java代码打包为可执行jar包后需要查看工程结构是否是且只有我们需要的包,故需要查看jar包层级. 1.windows系统可以直接在网上下载jd-gui.exe包,然后傻瓜安装: 2.Ma ...

  3. php中的花括号使用详解

    1.简单句法规则(用花括号界定变量名,适用于PHP所有版本,是php系统设定):    $a = 'flower';    echo "She received some $as" ...

  4. windows安装 阿里云的Fun工具

    由于项目使用到了 函数计算,特此了解到了需要安装 阿里云的Fun工具 Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算.API 网关.日志服务等资源.它通过一个 ...

  5. C语言实现读取文件所有内容到字符串

    #include "stdio.h" #include "string" #include "stdlib.h" using namespa ...

  6. 四、附加到进程调试(.NET Core)

    1.安装.net core windows server托管工具包: 1.下载https://dotnet.microsoft.com/download/thank-you/dotnet-runtim ...

  7. zabbix入门之监控MySQL

    zabbix入门之监控MySQL 这里使用的是zabbix官方自带的MySQL监控模板. 首先确保在被监控主机安装zabbix-agent.zabbix-sender,并且将主机加入监控节点.具体操作 ...

  8. CentOS 7 LNMP环境搭建 Zabbix3.4

    概述:在CentOS 7 64位操作系统环境下搭建LNMP(Linux+Nginx+MySQL+PHP)来运行Zabbix 3.4 监控程序 预先安装: yum install -y autoconf ...

  9. ppt怎么制作抖音快手快闪效果的倒计时动画?

    ppt怎么制作快闪效果的倒计时动画? 1.首先,我们新建一个ppt,如下图: 2.然后我们在ppt中插入一个文本,文本内容为3,如下图: 3.然后我们将我们的文本设置为“Arial Black”,如下 ...

  10. CF 36E Two Paths

    传送门 真实的自闭= =+ 考试的时候老师明明说了可以路径为空T^T 然后光荣的挂掉了 20分的链[明明是最送分的] 上来就看出来欧拉回路了嘛 然后思考了一下大概奇点配个对 删一条简单路径剩下的跑欧拉 ...