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();

}

No comments:

Post a Comment