标签:android style blog color io os 使用 ar for
cocos2dx2.2添加音乐音效以及设置音量(非原创)
cocos2d-x使用SimpleAudioEngine类作为实现跨平台的音乐引擎,首先需要引入头文件。
对于声音文件,我们可以提前加载,这样会提高游戏的执行效率,但却会增加内存消耗,编码时,我们需要根据实际情况决定是否要进行预加载处理。
好了,我们写一个例子先,同样还是新建一个Music项目。
1、引入头文件以及相应的库
#include "cocos2d.h" #include "SimpleAudioEngine.h" using namespace CocosDenshion;
2、预加载音乐文件(可以不进行提前加载)
1. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) 2. #define EFFECT_FILE "effect2.ogg" 3. #elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) 4. #define EFFECT_FILE "effect1.raw" 5. #else 6. #define EFFECT_FILE "effect1.wav" 7. #endif 8. 9. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 10. #define MUSIC_FILE "music.mid" 11. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX ) 12. #define MUSIC_FILE "background.ogg" 13. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) 14. #define MUSIC_FILE "background.wav" 15. #else 16. #define MUSIC_FILE "background.mp3" 17. #endif
3、使用音乐以及音效,以及控制音量
我们更改了HelloWorld的退出按钮,可以根据flag的值来做相应的音乐处理。
1. void HelloWorld::menuCloseCallback(CCObject * pSender) 2. {int flag = 0; //我这里直接定义一个flag,大家可根据游戏项目的需要传入flag的值 3. switch(flag) 4. { 5. // 背景音乐部分: 6. case 0: 7. SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true); //播放 8. break; 9. case 1: 10. SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); // 停止播放 11. break; 12. case 2: 13. SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //暂停播放 14. break; 15. case 3: 16. SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // 恢复播放 17. break; 18. case 4: 19. SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic(); //从头重新播放 20. break; 21. case 5: 22. if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) //判断当前是否播放背景音乐 23. { 24. CCLOG("background music is playing"); 25. } 26. else 27. { 28. CCLOG("background music is not playing"); 29. } 30. break; 31. // 音效部分 32. case 6: 33. unsigned int m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE); //播放 34. break; 35. case 7: 36. unsigned int m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true); 37. break; 38. case 8: 39. SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId); //停止播放 40. break; 41. case 9: 42. SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE); //去除音效 43. break; 44. // add bakcground music volume 45. case 10: 46. SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);//增加音量 47. break; 48. case 11: 49. SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);//降低音量 50. break; 51. case 12: 52. SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);//增加音量 53. break; 54. case 13: 55. SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);//降低音量 56. break; 57. case 14: 58. SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);//暂停音效 59. break; 60. case 15: 61. SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);//恢复音效 62. break; 63. case 16: 64. SimpleAudioEngine::sharedEngine()->pauseAllEffects();//暂停所有音效 65. break; 66. case 17: 67. SimpleAudioEngine::sharedEngine()->resumeAllEffects();//恢复所有音效 68. break; 69. case 18: 70. SimpleAudioEngine::sharedEngine()->stopAllEffects();//停止所有音效 71. break; 72. } 73. 74. }
标签:android style blog color io os 使用 ar for
原文地址:http://www.cnblogs.com/alenoscar/p/4022878.html