本文共 2209 字,大约阅读时间需要 7 分钟。
cocos2d-x 游戏中声音 有两种 一种是背景音乐一种是音效 载入音乐 或者音效的时候 我们须要先缓存声音
#include "SimpleAudioEngine.h"using namespace CocosDenshion;#define EFFECT_FILE "effect1.wav" //音效#define MUSIC_FILE "background.mp3" //音乐//这两个宏代表了 音乐和音效的名称或者文件夹SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE) );//缓存音乐SimpleAudioEngine::sharedEngine()->preloadEffect( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE) );//缓存音效//set default volumeSimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);//设置音效声音SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);//设置音乐声音
音乐部分
1.播放背景音乐SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);2.也能够推断眼下有没有背景音乐if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) { CCLOG("背景音乐正在播放"); } else { CCLOG("没有背景音乐播放"); } 3.停止背景音乐SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); 4.暂停背景音乐SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 5.恢复背景音乐SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 6.重头调用背景音乐SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();音效部分
1.播放音效m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str()); 2.反复播放音效m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str(), true); 3.停止播放音效SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId); 4.卸载音效SimpleAudioEngine::sharedEngine()->unloadEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE)).c_str()); 5.暂停音效SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId); 6.恢复音效SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId); 7.暂停全部音效SimpleAudioEngine::sharedEngine()->pauseAllEffects(); 8.恢复全部音效 SimpleAudioEngine::sharedEngine()->resumeAllEffects(); 9.停止全部音效 SimpleAudioEngine::sharedEngine()->stopAllEffects();
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5057010.html,如需转载请自行联系原作者