在玩游戏时都会有各种游戏音,如启动游戏时会有背景音,胜利或者失败会有一些音效,在Cocos2d-X中可以使用CocosDenshion实现各种声音
在使用CocosDenshion之前需要在程序中添加一个头文件和一个命名空间
- #include "SimpleAudioEngine.h"
- using namespace CocosDenshion;
播放背景音乐
停止播放背景音乐
- //停止播放背景音乐
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
停止播放音效
- //停止播放编号为effectID的音效
- CocosDenshion::SimpleAudioEngine::sharedEngine()->stopEffect(effectID);
设置背景音乐的音量
- //设置背景音乐的音量
- //音量的范围:0~1
- CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(.5f);
设置音效的音量
- //设置音效的音量
- //音量的范围:0~1
- CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(.5f);
加速缓存背景音乐
- CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("OnLand.wma");
加速缓存音效
- CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("EatCoin.wma");