Cocos2D對音樂/音效播放的封裝已經很好了,下邊是例子:
定義2個文件:
#define MUSIC_FILE "LOLBgMusic.mp3"
#define EFFECT_FILE "Attack.wav"
在初始化的時候預加載音樂/音效文件:
//預加載
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
//設置音量
CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
播放:
//循環播放背景音樂,
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true);
//播放音效
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
其他相關函數:
// 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();
轉載請註明出處:http://blog.csdn.net/Vestigge