欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

cocos2d-x音乐系统封装及使用

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: 
http://www.firedragonpzy.com.cn/index.php/archives/2984

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
【加群请写:Software MyZone或者是firedragonpzy】
群论坛:火龙论坛正试运营阶段,欢迎大家多提些建设性意见……

之前针对1.x的做了一套音乐系统的封装,针对cocos2d-x2.0.4,我简单的做了一下梳理【由于时间原因】,这个主要是针对项目,不过你可以提取一下。现在也基本差不多了,呵呵……

UtilTools.h

//
 // UtilTools.h
 // ToyGame
 // Created by firedragonpzy on 13-1-4.
 //
#ifndef ToyGame_UtilTools_h
 #define ToyGame_UtilTools_h
 #include "cocos2d.h"
 #include "SimpleAudioEngine.h"
 #include "MySource.h"
using namespace CocosDenshion
using namespace cocos2d;
class UtilTools
 {
 public:
 static bool musicState;
 static bool isMusicOpen;
static void playMusicAndEffect( const char* pszFilePath, bool bLoop )

{

setIntegerForKey(XML_MUSIC_STATUS, FDTRUE);

SimpleAudioEngine::sharedEngine()->playBackgroundMusic(pszFilePath, bLoop);

setIntegerForKey(XML_EFFECT_STATUS, FDTRUE);

UtilTools::musicState = true;

}

static void stopMusicAndEffect( const char* pszFilePath )

{

setIntegerForKey(XML_MUSIC_STATUS, FDFALSE);

SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(MUSIC_BG);

setIntegerForKey(XML_EFFECT_STATUS, FDFALSE);

UtilTools::musicState = false;

}

static void pauseMusicAndEffect()

{

setIntegerForKey(XML_MUSIC_STATUS, FDFALSE);

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

setIntegerForKey(XML_EFFECT_STATUS, FDFALSE);

SimpleAudioEngine::sharedEngine()->pauseAllEffects();

UtilTools::musicState = false;

}

static void resumeMusicAndEffect( const char* pszFilePath, bool bLoop )

{

if( UtilTools::isMusicOpen )

{

setIntegerForKey(XML_MUSIC_STATUS, FDTRUE);

setIntegerForKey(XML_EFFECT_STATUS, FDTRUE);

SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

SimpleAudioEngine::sharedEngine()->resumeAllEffects();

}else

{

playMusicAndEffect( pszFilePath, bLoop );

UtilTools::isMusicOpen = true;

}

UtilTools::musicState = true;

}


static void playEffect( const char* pszFilePath )

{

if (getIntegerForKey(XML_EFFECT_STATUS)) {

SimpleAudioEngine::sharedEngine()->playEffect( std::string(CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pszFilePath)).c_str() );

}

}

其中,musicState是针对home键的,代码可以如下操作:

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too

void AppDelegate::applicationDidEnterBackground()

{

CCDirector::sharedDirector()->stopAnimation();

// if you use SimpleAudioEngine, it must be paused

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();

//    UtilTools::pauseMusicAndEffect();

}

// this function will be called when the app is active again

void AppDelegate::applicationWillEnterForeground()

{

CCDirector::sharedDirector()->startAnimation();

// if you use SimpleAudioEngine, it must resume here

// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

if ( UtilTools::musicState ) {

SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

}

}

isMusicOpen主要是针对resumeBackgroundMusic来处理的,主要使用在:

if (CCUserDefault::sharedUserDefault()->getIntegerForKey(XML_ISFIRSTPALY) == FDFALSE) {

CCUserDefault::sharedUserDefault()->setIntegerForKey(XML_ISFIRSTPALY,FDTRUE);

CCUserDefault::sharedUserDefault()->flush();

UtilTools::playMusicAndEffect( MUSIC_BG, true );

UtilTools::isMusicOpen = true;

}else

{

if(UtilTools::getIntegerForKey(XML_MUSIC_STATUS) == FDTRUE)

{

UtilTools::playMusicAndEffect(MUSIC_BG, true);

UtilTools::isMusicOpen = true;

}

else

{

UtilTools::stopMusicAndEffect(MUSIC_BG);

UtilTools::isMusicOpen = false;

}

}

音乐按钮的操作如下;

void MainMenuScene::musicPress(CCObject* pSender)

{

UtilTools::playEffect( EFFECT_MENU );

if (musicFlag == 0) {

m_pMusicItemImage = (CCMenuItemImage*)pSender;

m_pMusicItemImage->selected();

UtilTools::pauseMusicAndEffect();

musicFlag =1;

}else if (musicFlag == 1)

{

m_pMusicItemImage = (CCMenuItemImage*)pSender;

m_pMusicItemImage->unselected();

musicFlag =0;

UtilTools::resumeMusicAndEffect( MUSIC_BG, true );

}

}

如果你按照这里来,目前为止我没有测出bug,有的朋友可以从群里通知我一声,或直接qq,不胜感激……

  1. Pingback: cocos2d-x音乐系统封装及使用 - 移动端开发 - 无忧网

  2. Pingback: cocos2d-x音乐系统封装及使用 - 移动端开发 - 开发者

  3. 我觉得你应该在每一篇文章的时候都加个视频或图片来显示效果~