Cocos2dx 下面,对 音乐 和音效简单封装

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();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值