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