声音

来源:互联网 发布:mac 文件夹权限 编辑:程序博客网 时间:2024/06/02 09:19

1、simlegame 中背景音乐和动作音效

详见:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_6_-_How_to_Play_Music_and_Sound_Effect

In this chapter, we would add background music to the game and play sound effect when the hero fires bullets.

Because there’s so few codes to add that we could say a little more about audio engine here. Cocos2d-x has wrapped SimpleAudioEngine to cross platforms. In our game, we are able to play music and sound effect using only one line of codes. It is so convenient. Of course, audio formats supported in different platform are different, about this issue, you could refer tohttp://www.cocos2d-x.org/projects/cocos2d-x/wiki/Audio_formats_supported_by_CocosDenshion_on_different_platforms.

In fact, cocos2d-iphone includes the cocosDenshion library, and the library offers three layers of interfaces: CDSoundEngine->CDAudioManager->SimpleAudioEngine, but its realization totally relies on OpenAL. About OpenAL, it is not a standard of Khronos Group, it is an open source of Creative, which could be realize by software and hardware. So far, OpenAL is hardware realized only by Apple’s products, so in other platforms, we couldn’t provide supports for the lower layers of cocosDenshion, but we support the top layer which is used most commonly by the developers.

Now let’s get right to the issues.
First, copy the sound files background-music-aac.wav and pew-pew-lei.wav to the Resource directory. We use wav here because wav is supported in all platforms and these two files have been included in the Cocos2dSimpleGame project, you could download them from the bottom of this page.
Then include SimpleaudioEngine.h in HelloWorldScene.cpp.

#include "SimpleAudioEngine.h" 

Add the background music in init().

CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true);   

And play the sound effect in ccTouchesEnded() when the bullet is fired.

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");  

Ok, the audio addition is completed now.

2、详细使用

详见:http://blog.csdn.net/yangyu20121224/article/details/10228941

官网api:http://www.cocos2d-x.org/reference/native-cpp/V2.1.4/de/d8f/class_cocos_denshion_1_1_simple_audio_engine.html#a91013ef8ca9544db243e255161e15c1c

    由于Cocos2D-X是跨平台的引擎,所以如果大家想通过一套代码多平台通用播放音乐与音效,可以通过Cocos2D-X

定义的宏CC_TARGET_PLATFORM平台变量来区别播放的音频格式。比如,Android只支持ogg的音频格式。

    SimpleAudioEngine的使用比较简单,此类不需要创建,只需要调用其中的播放、暂停、重复播放等与音乐、音效

相关的函数即可。

实例

代码:

class HelloWorld : public cocos2d::CCLayer{public:    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();      // there's no 'id' in cpp, so we recommand to return the exactly class pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuBack(CCObject* pSender);//用于记录音效IDint effectId;    // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);};

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"using namespace cocos2d;using namespace CocosDenshion;  //根据不同平台使用预编译索引不同音频文件  #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  #define EFFECT_FILE        "effect2.ogg"  #elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)  #define EFFECT_FILE        "effect1.raw"  #else  #define EFFECT_FILE        "effect1.wav"  #endif  #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  #define MUSIC_FILE        "music.mid"  #elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)  #define MUSIC_FILE        "background.ogg"  #else  #define MUSIC_FILE        "background.mp3"  #endif  std::string items[] = {  "play background music",  "stop background music",  "pause background music",  "resume background music",  "rewind background music",  "is background music playing",  "play effect",  "play effect repeatly",  "stop effect",  "unload effect",  "add background music volume",  "sub background music volume",  "add effects volume",  "sub effects volume",  "pause effect",  "resume effect",  "pause all effects",  "resume all effects",  "stop all effects"  }; CCScene* HelloWorld::scene(){    CCScene * scene = NULL;    do     {        // 'scene' is an autorelease object        scene = CCScene::create();        CC_BREAK_IF(! scene);        // 'layer' is an autorelease object        HelloWorld *layer = HelloWorld::create();        CC_BREAK_IF(! layer);        // add layer as a child to scene        scene->addChild(layer);    } while (0);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    bool bRet = false;do   {  CC_BREAK_IF(! CCLayer::init());  CCMenu* m_pItmeMenu = CCMenu::create();  CCSize s = CCDirector::sharedDirector()->getWinSize();  int m_nTestCount = sizeof(items) / sizeof(items[0]);  for (int i = 0; i < m_nTestCount; i++){  CCLabelTTF* label = CCLabelTTF::create(items[i].c_str(), "Arial", 15);  CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(HelloWorld::menuBack));  m_pItmeMenu->addChild(pMenuItem, i);  pMenuItem->setPosition( CCPointMake(0, (s.height*0.5-20- (i + 1) * 15) ));  }  addChild(m_pItmeMenu,0,100);  //预加载音乐和音效  SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);  SimpleAudioEngine::sharedEngine()->preloadEffect(EFFECT_FILE);  //设置默认音量  SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);  SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);  bRet = true;  } while (0);      return bRet;}void HelloWorld::menuBack(CCObject * pSender){  CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);  int nIdx = pMenuItem->getZOrder() ;  switch(nIdx)  {  case 0:  //播放背景音乐  SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE , true);  break;  case 1:  //停止背景音乐  SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  break;  case 2:  //暂停背景音乐  SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  break;  case 3:  //继续播放背景音乐  SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  break;  case 4:  //后退背景音乐  SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();  break;  case 5:  //背景音乐是否正在播放  if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()){  CCLOG("背景音乐正在播放");  }else{  CCLOG("背景音乐没在播放");  }  break;  case 6:  //播放音效,并且得到此音效的ID  effectId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);  break;  case 7:  //重复播放音效  effectId = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);  break;  case 8:  //停止音效播放  SimpleAudioEngine::sharedEngine()->stopEffect(effectId);  break;  case 9:  //释放音效  SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);  break;  case 10:  //增加背景音乐的音量  SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);  break;  case 11:  //减少背景音乐的音量  SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);  break;  case 12:  //增加音效的音量  SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);  break;  case 13:  //减少音效的音量  SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);  break;  case 14:  //暂停音效  SimpleAudioEngine::sharedEngine()->pauseEffect(effectId);  break;  case 15:  //继续播放音效  SimpleAudioEngine::sharedEngine()->resumeEffect(effectId);  break;  case 16:  //暂停所有音效  SimpleAudioEngine::sharedEngine()->pauseAllEffects();  break;  case 17:  //继续所有音效  SimpleAudioEngine::sharedEngine()->resumeAllEffects();  break;  case 18:  //停止所有音效  SimpleAudioEngine::sharedEngine()->stopAllEffects();  break;  }  }  

效果:


原创粉丝点击