在cocos2dx 关闭音效很多人都不知道如何去操作,下面是我们就实例演示:
我们可以使用Cocos2d-x自带的CocosDension库来使用声音引擎。CocosDension实现了简单易用的SimpleAudioEngin类,为了使用它,我们只需要引入它的头文件即可:
#include "SimpleAudioEngine.h" using namespace CocosDenshion; |
支持平台与格式
CocosDension支持的音乐格式如下:
平台 |
支持的常见文件格式 |
备注 |
Android |
mp3,mid,oggg,wav |
可以播放android,media,MediaPlayer所支持的所有格式 |
ios |
acc,caf,mp3,wav |
可以播放AVAudioPlayer所支持的所有格式 |
Windows |
mid,mp3,wav |
无 |
CocosDension支持的音效格式如下:
平台 |
支持的常见文件格式 |
备注 |
Android |
aggg,wav |
对wav的支持不完美 |
Ios |
caf,m4a |
可以播放Cocos2d-iphone,CocosDesion所支持的所有格式 |
windows |
mid,wav |
无 |
4 预加载
加载音乐和音效通常是一个耗时间的过程,因此为了防止由加载产生的延时导致实际播放与游戏播放不协调的现象。在播放音效和音乐前,需要预加载音乐文件。
通常我们会在进入场景前调用一下两个方法来预加载文件。
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("audio/start.wav"); SimpleAudioEngine::sharedEngine()->preloadEffect(“audio/onclick.wav”); |
5 播放与停止Vs暂停和恢复
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(“audio/start.wav”); |
bool T19Music::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { SimpleAudioEngine::sharedEngine()->playEffect("audio/onclick.wav"); static bool flag = true;
if (flag) { SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); } else { SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); //SimpleAudioEngine::sharedEngine()->playBackgroundMusic("audio/start.wav"); } flag = !flag; return true; } |
6 其它成员
volumn范围为[0,1]
//设置音量大小 SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.1f); //获得音量的大小 SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume(); |
7 进入后台或从后台进入
void AppDelegate::applicationDidEnterBackground() { CCDirector::sharedDirector()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); }
// 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(); } |
8 案例说明:
T20Music.h |
#ifndef __T20Music_H__ #define __T20Music_H__
#include "cocos2d.h" #include "TBack.h" USING_NS_CC; class T20Music :public TBack { public: static CCScene * scene(); CREATE_FUNC(T20Music); bool init();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); };
#endif
|
T20Music.cpp |
#include "T20Music.h" #include "AppMacros.h" #include "SimpleAudioEngine.h" using namespace CocosDenshion;
CCScene *T20Music::scene() { CCScene *scene = CCScene::create(); T20Music * layer = T20Music::create(); scene->addChild(layer); return scene; }
bool T20Music::init() { TBack::init(); //预加载音乐 SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("audio/start.wav"); SimpleAudioEngine::sharedEngine()->preloadEffect("audio/onclick.wav"); SimpleAudioEngine::sharedEngine()->playBackgroundMusic("audio/start.wav");
//设置触摸可以行 setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); return true; }
//通过鼠标事件控制声音的显示和消失 bool T20Music::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { static bool flag = true; SimpleAudioEngine::sharedEngine()->playEffect("audio/onclick.wav"); if (flag) { //SimpleAudioEngine::sharedEngine()->playBackgroundMusic("audio/start.wav"); //暂停播放音乐 SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } else { //SimpleAudioEngine::sharedEngine()->stopBackgroundMusic("audio/start.wav"); //重新恢复播放音乐 SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }
flag = !flag; //控制音量 SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.1f);
SimpleAudioEngine::sharedEngine()->stopAllEffects(); SimpleAudioEngine::sharedEngine()->unloadEffect("audio/start.wav");
return false; } |
运行效果是:当进入这个scene之后就会听见音乐,当鼠标点击的时候声音停止了。 |