- typedefstruct{
- unsignedintsoundId;
- constchar*soundFilePath;
- }Sound;
-
-
- char*musicFilePath;
- }Music;
-
-
- *
- */
- staticMusicbgMusic=
- {
- "audio/mainbgmusic.ogg"
- };
- staticSoundtestSound=
- 0,
- "audio/clicksound.ogg"
- };
- classAudioManager
- public:
- /*注意,此方法在进入游戏前调用*/
- staticvoidinit();
- /*预加载*/
- voidpreLoadBgMusic(Music&music);
- voidpreLoadSound(Sound&sound);
- /*播放*/
- voidplayMusic(Music&music,boolloop=true);
- voidplaySound(Sound&sound,153); background-color:inherit; font-weight:bold">false);
- /*暂停*/
- voidpauseMusic(Music&music);
- voidpauseSound(Sound&sound);
- /*停止*/
- voidstopMusic(Music&music,87); background-color:inherit; font-weight:bold">boolrelease=voidstopSound(Sound&sound);
- voidstopAllSound();
- /*恢复*/
- voidresumeMusic();
- /*全部结束*/
- voidend();
- </pre><br>
- <pre></pre>
- <br>
- <prename="code"class="cpp"><prename="code"class="cpp">voidAudioManager::init()
- {
- /*例子*/
- preLoadBgMusic(bgMusic);
- preLoadSound(testSound);
- }
- /*********************************************************
- 一些方法的封装
- *********************************************************/
- voidAudioManager::preLoadBgMusic(Music&music)
- usingnamespaceCocosDenshion;
- namespacecocos2d;
- music.musicFilePath=CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(music.musicFilePath);
- CCLOG(music.musicFilePath);
- SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(music.musicFilePath);
- voidAudioManager::preLoadSound(Sound&sound)
- namespaceCocosDenshion;
- namespacecocos2d;
- sound.soundFilePath=CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(sound.soundFilePath);
- SimpleAudioEngine::sharedEngine()->preloadEffect(sound.soundFilePath);
- voidAudioManager::playMusic(Music&music,87); background-color:inherit; font-weight:bold">boolloop)
- /*先判断是否已经在播放,如果在播放,则直接返回*/
- if(SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()){
- return;
- }
- SimpleAudioEngine::sharedEngine()->playBackgroundMusic(music.musicFilePath,loop);
- voidAudioManager::playSound(Sound&sound,0); background-color:inherit">/*defaultisfalse*/)
- sound.soundId=SimpleAudioEngine::sharedEngine()->playEffect(sound.soundFilePath,loop);
- voidAudioManager::pauseMusic(Music&music)
- SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- voidAudioManager::pauseSound(Sound&sound)
- SimpleAudioEngine::sharedEngine()->pauseEffect(sound.soundId);
- voidAudioManager::stopMusic(Music&music,87); background-color:inherit; font-weight:bold">boolrelease)
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(release);
- voidAudioManager::stopSound(Sound&sound)
- SimpleAudioEngine::sharedEngine()->stopEffect(sound.soundId);
- voidAudioManager::stopAllSound()
- SimpleAudioEngine::sharedEngine()->stopAllEffects();
- voidAudioManager::resumeMusic()
- SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- voidAudioManager::end()
- SimpleAudioEngine::sharedEngine()->end();
- 注意:
- 进入游戏后,开始初始化资源调用
- /*初始化游戏音效*/
- AudioManager::init();
-
-
- 游戏切换到后台调用:
- AudioManager::pauseMusic(bgMusic);
- 游戏重新切换回前台
- AudioManager::resumeMusic();
原文链接:https://www.f2er.com/cocos2dx/346716.html