void HelloWorld::play_toggle() { std::cout << "Touched" << std::endl; auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); if (this->playing) { audio->pauseBackgroundMusic(); } else { audio->resumeBackgroundMusic(); } this->playing = !this->playing; std::cout << this->playing << std::endl; }
bool AudioScene::init() { if (!Layer::init()) { return false; } auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->preloadBackgroundMusic("hungerland.wav"); audio->playBackgroundMusic("hungerland.wav"); auto eventListener = cocos2d::EventListenerKeyboard::create(); eventListener->onKeyPressed = [audio](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event) { switch (keyCode) { case cocos2d::EventKeyboard::KeyCode::KEY_SPACE: { if (audio->isBackgroundMusicPlaying()) { audio->pauseBackgroundMusic(); } else { audio->resumeBackgroundMusic(); } break; } case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW: { audio->playBackgroundMusic("noncombat.wav"); break; } case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW: { audio->playBackgroundMusic("hungerland.wav"); break; } } }; _eventDispatcher->addEventListenerWithFixedPriority(eventListener, 2); return true; }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (0 != strcmp(s_currentBackgroundStr.c_str(), pszFilePath)) { stopBackgroundMusic(true); } else { if (s_playStatus == PAUSED) resumeBackgroundMusic(); else rewindBackgroundMusic(); } if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); if (bLoop) { // set it up to loop strm_dict_t *dictionary = strm_dict_new(); s_repeatDictionary = strm_dict_set(dictionary, "repeat", "all"); if (mmr_input_parameters(s_mmrContext, s_repeatDictionary) != 0) { mmrerror(s_mmrContext, "input parameters (loop)"); return; } } if (s_hasMMRError || !s_mmrContext) return; if (mmr_play(s_mmrContext) < 0) { mmrerror(s_mmrContext, "mmr_play"); s_hasMMRError = true; } if (!s_hasMMRError) s_playStatus = PLAYING; }