首页 > 代码库 > cocos2dx音乐与音效
cocos2dx音乐与音效
// 预加载音乐和音效
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("fashu1.wav","fashu1.wav"));
SimpleAudioEngine::sharedEngine()->preloadEffect(CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("fashu1.wav","fashu1.wav"));
// 设置默认音量
SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
// 播放背景音乐
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("fashu1.wav","fashu1.wav"));
// 停止背景音乐
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
// 暂停背景音乐
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
// 继续播放背景音乐
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
// 后退背景音乐
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
// 背景音乐是否正在播放
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{ CCLOG("正在播放"); }
else CCLOG("没在播放");
// 播放音效,并且得到此音效的ID
SimpleAudioEngine::sharedEngine()->playEffect(CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("fashu1.wav","fashu1.wav"));
// 重复播放音效
SimpleAudioEngine::sharedEngine()->playEffect(std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str(), true);
// 停止播放音效
SimpleAudioEngine::sharedEngine()->stopEffect(effectId);
// 释放音效
SimpleAudioEngine::sharedEngine()->unloadEffect (std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE)).c_str() );
// 增加背景音乐音量
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()+0.1f);
// 减少背景音乐音量
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()-0.1f);
// 增加背景音效音量
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()+0.1f);
// 减少背景音效音量
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume()-0.1f);
// 暂停音效
SimpleAudioEngine::sharedEngine()->pauseEffect(effectId);
// 继续 播放音效
SimpleAudioEngine::sharedEngine()->resumeEffect(effectId);