123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- /****************************************************************************
- Copyright (c) 2013-2017 Chukong Technologies Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "editor-support/cocostudio/CCComAudio.h"
- #include "audio/include/SimpleAudioEngine.h"
- #include "platform/CCFileUtils.h"
- namespace cocostudio {
- IMPLEMENT_CLASS_COMPONENT_INFO(ComAudio)
- const std::string ComAudio::COMPONENT_NAME = "CCComAudio";
- ComAudio::ComAudio()
- : _filePath("")
- , _loop(false)
- , _startedSoundId(0)
- {
- _name = COMPONENT_NAME;
- }
- ComAudio::~ComAudio()
- {
-
- }
- bool ComAudio::init()
- {
- return true;
- }
- void ComAudio::onEnter()
- {
- }
- void ComAudio::onExit()
- {
- stopBackgroundMusic(true);
- stopAllEffects();
- }
- void ComAudio::onAdd()
- {
- }
- void ComAudio::onRemove()
- {
- stopBackgroundMusic(true);
- stopAllEffects();
- }
- bool ComAudio::serialize(void* r)
- {
- bool ret = false;
- do
- {
- CC_BREAK_IF(r == nullptr);
- SerData *serData = (SerData *)(r);
- const rapidjson::Value *v = serData->_rData;
- stExpCocoNode *cocoNode = serData->_cocoNode;
- CocoLoader *cocoLoader = serData->_cocoLoader;
- const char *className = nullptr;
- const char *comName = nullptr;
- const char *file = nullptr;
- std::string filePath;
- int resType = 0;
- bool loop = false;
- if (v != nullptr)
- {
- className = DICTOOL->getStringValue_json(*v, "classname");
- CC_BREAK_IF(className == nullptr);
- comName = DICTOOL->getStringValue_json(*v, "name");
- const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
- CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
- file = DICTOOL->getStringValue_json(fileData, "path");
- CC_BREAK_IF(file == nullptr);
- resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
- CC_BREAK_IF(resType != 0);
- loop = DICTOOL->getIntValue_json(*v, "loop") != 0? true:false;
- }
- else if (cocoNode != nullptr)
- {
- className = cocoNode[1].GetValue(cocoLoader);
- CC_BREAK_IF(className == nullptr);
- comName = cocoNode[2].GetValue(cocoLoader);
- stExpCocoNode *pfileData = cocoNode[4].GetChildArray(cocoLoader);
- CC_BREAK_IF(!pfileData);
- file = pfileData[0].GetValue(cocoLoader);
- CC_BREAK_IF(file == nullptr);
- resType = atoi(pfileData[2].GetValue(cocoLoader));
- CC_BREAK_IF(resType != 0);
- loop = atoi(cocoNode[5].GetValue(cocoLoader)) != 0? true:false;
- ret = true;
- }
- if (comName != nullptr)
- {
- setName(comName);
- }
- else
- {
- setName(className);
- }
- if (file != nullptr)
- {
- if (strcmp(file, "") == 0)
- {
- continue;
- }
- filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
- }
- if (strcmp(className, "CCBackgroundAudio") == 0)
- {
- preloadBackgroundMusic(filePath.c_str());
- setLoop(loop);
- playBackgroundMusic(filePath.c_str(), loop);
- }
- else if(strcmp(className, COMPONENT_NAME.c_str()) == 0)
- {
- preloadEffect(filePath.c_str());
- }
- else
- {
- CC_BREAK_IF(true);
- }
- ret = true;
- }while (0);
- return ret;
- }
- ComAudio* ComAudio::create()
- {
- ComAudio * pRet = new (std::nothrow) ComAudio();
- if (pRet && pRet->init())
- {
- pRet->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(pRet);
- }
- return pRet;
- }
- void ComAudio::end()
- {
- CocosDenshion::SimpleAudioEngine::end();
- }
- void ComAudio::preloadBackgroundMusic(const char* pszFilePath)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(pszFilePath);
- setFile(pszFilePath);
- setLoop(false);
- }
- void ComAudio::playBackgroundMusic(const char* pszFilePath, bool loop)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, loop);
-
- }
- void ComAudio::playBackgroundMusic(const char* pszFilePath)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath);
- }
- void ComAudio::playBackgroundMusic()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(_filePath.c_str(), _loop);
- }
- void ComAudio::stopBackgroundMusic(bool bReleaseData)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(bReleaseData);
- }
- void ComAudio::stopBackgroundMusic()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
- }
- void ComAudio::pauseBackgroundMusic()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
- }
- void ComAudio::resumeBackgroundMusic()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
- }
- void ComAudio::rewindBackgroundMusic()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->rewindBackgroundMusic();
- }
- bool ComAudio::willPlayBackgroundMusic()
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->willPlayBackgroundMusic();
- }
- bool ComAudio::isBackgroundMusicPlaying()
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying();
- }
- float ComAudio::getBackgroundMusicVolume()
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();
- }
- void ComAudio::setBackgroundMusicVolume(float volume)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(volume);
- }
- float ComAudio::getEffectsVolume()
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume();
- }
- void ComAudio::setEffectsVolume(float volume)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(volume);
- }
- unsigned int ComAudio::playEffect(const char* pszFilePath, bool loop)
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath, loop);
- }
- unsigned int ComAudio::playEffect(const char* pszFilePath)
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath);
- }
- unsigned int ComAudio::playEffect()
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(_filePath.c_str(), _loop);
- }
- void ComAudio::pauseEffect(unsigned int nSoundId)
- {
- return CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(nSoundId);
- }
- void ComAudio::pauseAllEffects()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->pauseAllEffects();
- }
- void ComAudio::resumeEffect(unsigned int nSoundId)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->resumeEffect(nSoundId);
- }
- void ComAudio::resumeAllEffects()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->resumeAllEffects();
- }
- void ComAudio::stopEffect(unsigned int nSoundId)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(nSoundId);
- }
- void ComAudio::stopAllEffects()
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
- }
- void ComAudio::preloadEffect(const char* pszFilePath)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect(pszFilePath);
- setFile(pszFilePath);
- setLoop(false);
- }
- void ComAudio::unloadEffect(const char *pszFilePath)
- {
- CocosDenshion::SimpleAudioEngine::getInstance()->unloadEffect(pszFilePath);
- }
- void ComAudio::setFile(const char* pszFilePath)
- {
- _filePath.assign(pszFilePath);
- }
- void ComAudio::setLoop(bool loop)
- {
- _loop = loop;
- }
- const char* ComAudio::getFile()
- {
- return _filePath.c_str();
- }
- bool ComAudio::isLoop()
- {
- return _loop;
- }
- void ComAudio::start()
- {
- _startedSoundId = playEffect();
- }
- void ComAudio::stop()
- {
- stopEffect(_startedSoundId);
- }
- }
|