今天白白跟大家分享一下cocos2dx添加背景音乐音效的方法。
Cocos2d-x使用SimpleAudioEngine类作为实现跨平台的音乐引擎,首先需要引入头文件。
对于声音文件,我们可以提前加载,这样会提高游戏的执行效率,但却会增加内存消耗,编码时,我们需要根据实际情况决定是否要进行预加载处理。
好了,我们写一个例子先,同样还是新建一个Music项目。
1、引入头文件以及相应的库
- #include "cocos2d.h"
- #include "SimpleAudioEngine.h"
- #include "CocosDenshionTest.h"
- using namespace CocosDenshion;
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- #define EFFECT_FILE "effect2.ogg"
- #elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
- #define EFFECT_FILE "effect1.raw"
- #else
- #define EFFECT_FILE "effect1.wav"
- #endif
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- #define MUSIC_FILE "music.mid"
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX )
- #define MUSIC_FILE "background.ogg"
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
- #define MUSIC_FILE "background.wav"
- #else
- #define MUSIC_FILE "background.mp3"
- #endif
3、使用音乐以及音效,以及控制音量
我们更改了HelloWorld的退出按钮,可以根据flag的值来做相应的音乐处理。
- void HelloWorld::menuCloseCallback(CCObject * pSender)
- {int flag = 0; //我这里直接定义一个flag,大家可根据游戏项目的需要传入flag的值
- switch(flag)
- {
- // 背景音乐部分:
- case 0:
- SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true); //播放
- break;
- case 1:
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); // 停止播放
- break;
- case 2:
- SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //暂停播放
- break;
- case 3:
- SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // 恢复播放
- break;
- case 4:
- SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic(); //从头重新播放
- break;
- case 5:
- if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) //判断当前是否播放背景音乐
- {
- CCLOG("background music is playing");
- }
- else
- {
- CCLOG("background music is not playing");
- }
- break;
- // 音效部分
- case 6:
- unsigned int m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE); //播放
- break;
- case 7:
- unsigned int m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE,true);
- break;
- case 8:
- SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId); //停止播放
- break;
- case 9:
- SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE); //去除音效
- break;
- // add bakcground music volume
- case 10:
- SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);//增加音量
- break;
- case 11:
- SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);//降低音量
- break;
- case 12:
- SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);//增加音量
- break;
- case 13:
- SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);//降低音量
- break;
- case 14:
- SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);//暂停音效
- break;
- case 15:
- SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);//恢复音效
- break;
- case 16:
- SimpleAudioEngine::sharedEngine()->pauseAllEffects();//暂停所有音效
- break;
- case 17:
- SimpleAudioEngine::sharedEngine()->resumeAllEffects();//恢复所有音效
- break;
- case 18:
- SimpleAudioEngine::sharedEngine()->stopAllEffects();//停止所有音效
- break;
- }
- }
ok,关于音乐的所有函数都在上面了,大家可以根据自己的需要去使用啦。
来源网址:http://www.jb51.cc/article/p-ohhiwyjm-wc.html