Cocos2DX为我们提供了简单易用的音乐引擎,对于简单的使用只需要掌握libCocosDenshion库中的SimpleAudioEngine使用即可。
libCocosDenshion将音乐分为两个大类:背景音乐和音效
- 背景音乐:通常背景音乐时间较长;
- 音效:而音效通常在特定的动作执行时播放,时间较短,更换频率高。
下面就以背景音乐的实例为例看看怎么使用SimpleAudioEngine
新建工程DemoMusic,去掉不必要的并加载bg.mp3文件到工程的Resources目录下:
还是先看看SimpleAudioEngine为我们提供了那些方法:
//获取单例对象 static SimpleAudioEngine* sharedEngine(); //释放对象资源 static void end(); //预加载背景音乐 void preloadBackgroundMusic(const char* pszFilePath); //播放背景音乐,根据参数选择循环播放 void playBackgroundMusic(const char* pszFilePath, bool bLoop); //停止背景音乐 void stopBackgroundMusic(bool bReleaseData); //暂停 void pauseBackgroundMusic(); //恢复 void resumeBackgroundMusic(); //获取音量大小 float getBackgroundMusicVolume(); //设置音量大小 void setBackgroundMusicVolume(float volume);
从方法名称就很容易的明白如何使用下面init方法修改如下:
bool HelloWorld::init() { float fontSize = 16,startY = 10 , offset=15; // // 1. super init first if ( !CCLayer::init() ) { return false; } CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("bg.mp3"); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenuItem* pPlayBGMusic = CCMenuItemLabel::create( CCLabelTTF::create("Play BG Music","HiraKakuProN-W6",fontSize) , this, menu_selector(HelloWorld::menuPlayBGMusic)); pPlayBGMusic->setPosition(ccp(visibleSize.width/2-pPlayBGMusic->getContentSize().width/2,startY)); CCMenuItem* pStopBGMusic = CCMenuItemLabel::create( CCLabelTTF::create("Stop BG Music","HiraKakuProN-W6",fontSize) , this, menu_selector(HelloWorld::menuStopBGMusic)); pStopBGMusic->setPosition(ccp(visibleSize.width/2-pStopBGMusic->getContentSize().width/2,startY+offset)); CCMenuItem* pPauseBGMusic = CCMenuItemLabel::create( CCLabelTTF::create("Pause BG Music","HiraKakuProN-W6",fontSize) , this, menu_selector(HelloWorld::menuPauseBGMusic)); pPauseBGMusic->setPosition(ccp(visibleSize.width/2-pPauseBGMusic->getContentSize().width/2,startY+offset*2)); CCMenuItem* pResumeBGMusic = CCMenuItemLabel::create( CCLabelTTF::create("Resume BG Music","HiraKakuProN-W6",fontSize) , this, menu_selector(HelloWorld::menuResumeBGMusic)); pResumeBGMusic->setPosition(ccp(visibleSize.width/2-pResumeBGMusic->getContentSize().width/2,startY+offset*3)); CCMenuItem* pVolumeAddBGMusic = CCMenuItemLabel::create( CCLabelTTF::create("Volume + BG Music","HiraKakuProN-W6",fontSize) , this, menu_selector(HelloWorld::menuVolumeAddBGMusic)); pVolumeAddBGMusic->setPosition(ccp(visibleSize.width/2-pVolumeAddBGMusic->getContentSize().width/2,startY+offset*4)); CCMenuItem* pVolumeSubBGMusic = CCMenuItemLabel::create( CCLabelTTF::create("Volume - BG Music","HiraKakuProN-W6",fontSize) , this, menu_selector(HelloWorld::menuVolumeSubBGMusic)); pVolumeSubBGMusic->setPosition(ccp(visibleSize.width/2-pVolumeSubBGMusic->getContentSize().width/2,startY+offset*5)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem,pPlayBGMusic,pStopBGMusic,pPauseBGMusic ,pResumeBGMusic,pVolumeAddBGMusic , pVolumeSubBGMusic , NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); return true; }
几个菜单回调函数:
void HelloWorld::menuVolumeSubBGMusic(CCObject* pSender) { float volume=CocosDenshion::SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume(); CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(volume-0.2); CCLOG ("volume - : %f", CocosDenshion::SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()); } void HelloWorld::menuVolumeAddBGMusic(CCObject* pSender) { float volume=CocosDenshion::SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume(); CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(volume+0.2); CCLOG ("volume + : %f", CocosDenshion::SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()); } void HelloWorld::menuResumeBGMusic(CCObject* pSender) { CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); } void HelloWorld::menuPauseBGMusic(CCObject* pSender) { CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } void HelloWorld::menuStopBGMusic(CCObject* pSender) { CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(false); } void HelloWorld::menuPlayBGMusic(CCObject* pSender) { CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg.mp3"); }
运行看看效果:
音量设置还有问题,貌似没有起到作用,待解!!