#include <SimpleAudioEngine.h>
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
const char* sound = "audio/CanBomb.mp3";
auto audio=CocosDenshion::SimpleAudioEngine::getInstance();
audio->preloadEffect(sound);
int soundId = audio->playEffect(sound,true);//true: looping sound
//create the action to delay 15 secs
auto actionDelay = DelayTime::create(15);
//create the action to pause sound
auto callbackAudio = CallFunc::create([](){
CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(soundId);
});
//create sequence putting in delay and pause action created above
auto actionSequence = Sequence::create(actionDelay,callbackAudio, NULL);
//run the action
//run the action
this->runAction(actionSequence);
return true;
}
This is another way to do it:
//create the sound and play loop it
#include <SimpleAudioEngine.h>
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
const char* sound = "audio/CanBomb.mp3";
auto audio=CocosDenshion::SimpleAudioEngine::getInstance();
audio->preloadEffect(sound);
int soundId = audio->playEffect(sound,true);//true: looping sound
this->schedule(schedule_selector(HelloWorld::StopSound), 15);
return true;
}
//Declare it in header file as well:
void HelloWorld::StopSound(float dt){
}
-----------------------------------------------------------------------
//The header file
class HelloWorld : public cocos2d::Layer
{
void StopSound(float dt);
};
//Declare it in header file as well:
void HelloWorld::StopSound(float dt){
CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(soundId);
}
-----------------------------------------------------------------------
//The header file
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
No comments:
Post a Comment