cocos2dx AudioEngine(声音播放)音效播放结束判断测试和背景音乐播放

由于simpleAudioEngine在播放音效时无法获取状态也无法在播放完成后回调,造成音效在按键频繁时发声重叠。改mac版本的库对目前的我来说挑战难度太大。

于是百度了半天,发现3.6版本的cocos2dx有了新的声音播放库AudioEngine。于是体验下,感觉还不错:

直接贴代码:

头文件:

#ifndef __macstudycocos2dx__newAudioEngineTest__

#define __macstudycocos2dx__newAudioEngineTest__


#include

#include "cocos2d.h"

#include "audio/include/AudioEngine.h"

USING_NS_CC;

using namespace experimental;


class newAudioEngineTest:publicLayer ,publicAudioEngine{

public:

   staticcocos2d::Scene* createScene();

    


   virtualbool init();

    

    // a selector callback

   void playCallback(cocos2d::Ref* pSender);// 点击按钮后开始播放的回调函数

    

 

    CREATE_FUNC(newAudioEngineTest);

    

   bool isplaying=false;//标记播放状态

    

   void changer_playingstatus();//播放完成后的回调函数,用来更改播放状态

    

    

};

newAudioEngineTest.cpp如下:


#endif /* defined(__macstudycocos2dx__newAudioEngineTest__) */


//

//  newAudioEngineTest.cpp

//  macstudycocos2dx

//

//  Created by WangWei on 15/6/23.

//

//


#include "newAudioEngineTest.h"


Scene* newAudioEngineTest::createScene()

{

    // 'scene' is an autorelease object

   auto scene =Scene::create();

    

    // 'layer' is an autorelease object

   auto layer =newAudioEngineTest::create();

    

    // add layer as a child to scene

    scene->addChild(layer);

    

    // return the scene

   return scene;

}

bool newAudioEngineTest::init()

{

    //////////////////////////////

    // 1. super init first

   if ( !Layer::init() )

    {

        return false;

    }

    

   Size visibleSize =Director::getInstance()->getVisibleSize();

    Vec2 origin =Director::getInstance()->getVisibleOrigin();

    

    

    auto menuitem=MenuItemFont::create("play wav", CC_CALLBACK_1(newAudioEngineTest::playCallback,this));

    //创建菜单项,点击时播放音效

    

    menuitem->setPosition(Vec2(0,0));

    

    // create menu, it's an autorelease object

   auto menu =Menu::create(menuitem,NULL);

    menu->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));

   this->addChild(menu,1);

    

   

    

    return true;

}



voidnewAudioEngineTest::playCallback(Ref* pSender)

{


   if (!isplaying) {

        log("wav is not playing!");

       isplaying=true;

       int audioID=play2d("man1.wav");//播放音效文件

       log("audioID =%d",audioID);

        setFinishCallback(audioID,CC_CALLBACK_0(newAudioEngineTest::changer_playingstatus,this));

        //音效播放完成后修改播放状态,此时会用到audioID

    }

   else{

        log("wav is playing,this  click is not response!");

    }

}


voidnewAudioEngineTest::changer_playingstatus(){

    isplaying=false;

    log("finished,status has been change");

}


以下是log输出:(可以看出audioID是改变的,即使播放同一个音效)


2015-06-23 15:24:35.303 macstudycocos2dx iOS[17888:662515] cocos2d: surface size: 1024x768

wav is not playing!

audioID =0

finished,status has been change

wav is not playing!

audioID =1

finished,status has been change

wav is not playing!

audioID =2

finished,status has been change

wav is not playing!

audioID =3

finished,status has been change

wav is not playing!

audioID =4

wav is playing,this  click is not response!   //快速点击时

finished,status has been change

wav is not playing!

audioID =5     

wav is playing,this  click is not response!   //快速点击时

wav is playing,this  click is not response!   //快速点击时

finished,status has been change

wav is not playing!

audioID =6

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

finished,status has been change

wav is not playing!

audioID =7

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

wav is playing,this  click is not response!

finished,status has been change


起初觉得如果音效可以使用AudioEngine来做,记住要用caf和wav格式。背景音乐可以使用simpleAudioEngine来播放,毕竟simpleAudioEngine有缓存机制。

更正下,之所以标红,是今天发现如果同时使用AudioEnginesimpleAudioEngine时会导致音效无法播放,具体原因不清楚。看来只能使用AudioEngine了。

 

AudioEngine中没有了playbackgoudmuisc的方法,背景音乐通过play2d来播放,通过AudioID可以对背景音乐的播放进行控制,用起来也很方便。 



而且,今天发现AudioEngine能播放mp3格式的音乐文件,对于被wav撑得硕大的app来说使用mp3格式无疑是个完美的解决方案,真机的调试效果也是相当满意。

 (未完待续)


如果不继承类时可以使用以下方式来播放哦:

int audioID=experimental::AudioEngine::play2d(wavfilestring->getCString());


你可能感兴趣的:(cocos2d-x)