typedef struct {
unsigned int soundId;
const char* soundFilePath;
}Sound;
typedef struct {
const char* musicFilePath;
} Music;
/*
* 注意,Andoird下面只支持.ogg
*
*/
static Music bgMusic =
{
"audio/mainbgmusic.ogg"
};
static Sound testSound =
{
0,
"audio/clicksound.ogg"
};
class AudioManager
{
public:
/* 注意,此方法在进入游戏前调用 */
static void init();
public:
/* 预加载 */
static void preLoadBgMusic(Music& music);
static void preLoadSound(Sound& sound);
/* 播放 */
static void playMusic(Music& music,bool loop = true);
static void playSound(Sound& sound,bool loop = false);
/* 暂停 */
static void pauseMusic(Music& music);
static void pauseSound(Sound& sound);
/* 停止 */
static void stopMusic(Music& music,bool release = true);
static void stopSound(Sound& sound);
static void stopAllSound();
/* 恢复 */
static void resumeMusic();
/* 全部结束 */
static void end();
};
void AudioManager::init()
{
/* 例子 */
preLoadBgMusic(bgMusic);
preLoadSound(testSound);
}
/*********************************************************
一些方法的封装
*********************************************************/
void AudioManager::preLoadBgMusic(Music& music)
{
using namespace CocosDenshion;
using namespace cocos2d;
music.musicFilePath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(music.musicFilePath);
CCLOG(music.musicFilePath);
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(music.musicFilePath);
}
void AudioManager::preLoadSound(Sound& sound)
{
using namespace CocosDenshion;
using namespace cocos2d;
sound.soundFilePath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(sound.soundFilePath);
SimpleAudioEngine::sharedEngine()->preloadEffect(sound.soundFilePath);
}
void AudioManager::playMusic(Music& music,bool loop/* default = true */)
{
using namespace CocosDenshion;
/* 先判断是否已经在播放,如果在播放,则直接返回 */
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) {
return;
}
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(music.musicFilePath, loop);
}
void AudioManager::playSound(Sound& sound,bool loop/* default is false */)
{
using namespace CocosDenshion;
sound.soundId = SimpleAudioEngine::sharedEngine()->playEffect(sound.soundFilePath, loop);
}
void AudioManager::pauseMusic(Music& music)
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
void AudioManager::pauseSound(Sound& sound)
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->pauseEffect(sound.soundId);
}
void AudioManager::stopMusic(Music& music,bool release/* default is true */)
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(release);
}
void AudioManager::stopSound(Sound& sound)
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->stopEffect(sound.soundId);
}
void AudioManager::stopAllSound()
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->stopAllEffects();
}
void AudioManager::resumeMusic()
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
void AudioManager::end()
{
using namespace CocosDenshion;
SimpleAudioEngine::sharedEngine()->end();
}
注意:
进入游戏后,开始初始化资源调用
/* 初始化游戏音效 */
AudioManager::init();
游戏切换到后台调用:
AudioManager::pauseMusic(bgMusic);
游戏重新切换回前台
AudioManager::resumeMusic();