Showing posts with label cocos2dx: audio. Show all posts
Showing posts with label cocos2dx: audio. Show all posts

Wednesday, May 6, 2015

Cocos2dx: Playing Background Music



bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    
    const char* music = "audio/game_music.mp3";
    CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(music);
    CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(music);
    CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.1);
    this->schedule(schedule_selector(HelloWorld::StopSound), 3);
    this->schedule(schedule_selector(HelloWorld::ResumeSound), 10);
    return true;
}

void HelloWorld::ResumeSound(float dt){
    CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

void HelloWorld::StopSound(float dt){
    
    CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();

}

Cocos2dx: Pausing a sound after finite seconds

This is one way to play loop a sound and then pausing it after 15 secs. See green comments for explanation.

#include <SimpleAudioEngine.h>

bool HelloWorld::init()
{
   
    if ( !Layer::init() )
    {
        return false;
    }
    
    
   
    const char* sound = "audio/CanBomb.mp3";
    
    //create the sound and play loop it
    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
    this->runAction(actionSequence);
    
    return true;
}


This is another way to do it:

#include <SimpleAudioEngine.h>

bool HelloWorld::init()
{
   
    if ( !Layer::init() )
    {
        return false;
    }
    
    
   
    const char* sound = "audio/CanBomb.mp3";
    
    //create the sound and play loop it
    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){
    
    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);
    
   
    
    void StopSound(float dt);

};