首页 > 代码库 > Cocos2d 播放音效
Cocos2d 播放音效
Cocos2D对音乐/音效播放的封装已经很好了,下边是例子:
定义2个文件:
[cpp] view plaincopy
- #define MUSIC_FILE "LOLBgMusic.mp3"
- #define EFFECT_FILE "Attack.wav"
在初始化的时候预加载音乐/音效文件:
[cpp] view plaincopy
- //预加载
- CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
- CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
- //设置音量
- CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
- CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
播放:
[cpp] view plaincopy
- //循环播放背景音乐,
- CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true);
[cpp] view plaincopy
- //播放音效
- CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
其他相关函数:
[cpp] view plaincopy
- // stop background music
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
- // pause background music
- SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- // resume background music
- SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- // rewind background music
- SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
- // is background music playing
- SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()
- // add bakcground music volume
- SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
- SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
- SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
- SimpleAudioEngine::sharedEngine()->pauseAllEffects();
- SimpleAudioEngine::sharedEngine()->resumeAllEffects();
- SimpleAudioEngine::sharedEngine()->stopAllEffects();
Cocos2d 播放音效
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。