axmol/cocos/editor-support/cocostudio/CCComAudio.cpp

336 lines
8.3 KiB
C++
Raw Normal View History

2013-06-04 17:38:43 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2013-06-04 17:38:43 +08:00
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"
2019-06-12 09:23:16 +08:00
#include "editor-support/cocostudio/SimpleAudioEngine.h"
2014-06-04 14:27:14 +08:00
#include "platform/CCFileUtils.h"
2013-06-04 17:38:43 +08:00
namespace cocostudio {
2013-06-04 17:38:43 +08:00
IMPLEMENT_CLASS_COMPONENT_INFO(ComAudio)
const std::string ComAudio::COMPONENT_NAME = "CCComAudio";
ComAudio::ComAudio()
: _filePath("")
, _loop(false)
2016-01-12 17:21:01 +08:00
, _startedSoundId(0)
2013-06-04 17:38:43 +08:00
{
_name = COMPONENT_NAME;
2013-06-04 17:38:43 +08:00
}
ComAudio::~ComAudio()
2013-06-04 17:38:43 +08:00
{
}
bool ComAudio::init()
2013-06-04 17:38:43 +08:00
{
return true;
}
void ComAudio::onEnter()
2013-06-04 17:38:43 +08:00
{
}
void ComAudio::onExit()
2013-06-04 17:38:43 +08:00
{
stopBackgroundMusic(true);
stopAllEffects();
}
void ComAudio::onAdd()
{
}
void ComAudio::onRemove()
{
stopBackgroundMusic(true);
stopAllEffects();
}
bool ComAudio::serialize(void* r)
{
2014-06-30 17:15:55 +08:00
bool ret = false;
2014-06-25 11:58:36 +08:00
do
{
2014-06-30 17:15:55 +08:00
CC_BREAK_IF(r == nullptr);
2014-06-25 18:10:06 +08:00
SerData *serData = (SerData *)(r);
const rapidjson::Value *v = serData->_rData;
stExpCocoNode *cocoNode = serData->_cocoNode;
2014-07-01 16:31:17 +08:00
CocoLoader *cocoLoader = serData->_cocoLoader;
2014-06-30 17:15:55 +08:00
const char *className = nullptr;
const char *comName = nullptr;
const char *file = nullptr;
2014-06-25 18:10:06 +08:00
std::string filePath;
int resType = 0;
bool loop = false;
2014-06-30 17:15:55 +08:00
if (v != nullptr)
{
2014-06-25 18:10:06 +08:00
className = DICTOOL->getStringValue_json(*v, "classname");
2014-06-30 17:15:55 +08:00
CC_BREAK_IF(className == nullptr);
2014-06-25 18:10:06 +08:00
comName = DICTOOL->getStringValue_json(*v, "name");
2014-06-25 11:58:36 +08:00
const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
2014-06-25 18:10:06 +08:00
file = DICTOOL->getStringValue_json(fileData, "path");
2014-06-30 17:15:55 +08:00
CC_BREAK_IF(file == nullptr);
2014-06-25 18:10:06 +08:00
resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
CC_BREAK_IF(resType != 0);
loop = DICTOOL->getIntValue_json(*v, "loop") != 0? true:false;
2014-06-25 11:58:36 +08:00
}
2014-06-30 17:15:55 +08:00
else if (cocoNode != nullptr)
2014-06-25 11:58:36 +08:00
{
2014-07-01 16:31:17 +08:00
className = cocoNode[1].GetValue(cocoLoader);
2014-06-30 17:15:55 +08:00
CC_BREAK_IF(className == nullptr);
2014-07-01 16:31:17 +08:00
comName = cocoNode[2].GetValue(cocoLoader);
stExpCocoNode *pfileData = cocoNode[4].GetChildArray(cocoLoader);
2014-06-25 11:58:36 +08:00
CC_BREAK_IF(!pfileData);
2014-07-01 16:31:17 +08:00
file = pfileData[0].GetValue(cocoLoader);
2014-06-30 17:15:55 +08:00
CC_BREAK_IF(file == nullptr);
2014-07-01 16:31:17 +08:00
resType = atoi(pfileData[2].GetValue(cocoLoader));
2014-06-25 18:10:06 +08:00
CC_BREAK_IF(resType != 0);
2014-07-01 16:31:17 +08:00
loop = atoi(cocoNode[5].GetValue(cocoLoader)) != 0? true:false;
2014-06-30 17:15:55 +08:00
ret = true;
2014-06-25 11:58:36 +08:00
}
2014-06-30 17:15:55 +08:00
if (comName != nullptr)
2014-06-25 11:58:36 +08:00
{
2014-06-25 18:10:06 +08:00
setName(comName);
}
else
{
2014-06-25 18:10:06 +08:00
setName(className);
}
2014-06-30 17:15:55 +08:00
if (file != nullptr)
{
2014-06-25 18:10:06 +08:00
if (strcmp(file, "") == 0)
2014-06-25 11:58:36 +08:00
{
continue;
}
2014-06-25 18:10:06 +08:00
filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
}
2014-06-25 18:10:06 +08:00
if (strcmp(className, "CCBackgroundAudio") == 0)
{
2014-06-25 18:10:06 +08:00
preloadBackgroundMusic(filePath.c_str());
setLoop(loop);
playBackgroundMusic(filePath.c_str(), loop);
}
else if(strcmp(className, COMPONENT_NAME.c_str()) == 0)
{
2014-06-25 18:10:06 +08:00
preloadEffect(filePath.c_str());
}
else
{
CC_BREAK_IF(true);
}
2014-06-30 17:15:55 +08:00
ret = true;
2014-06-25 11:58:36 +08:00
}while (0);
2014-06-30 17:15:55 +08:00
return ret;
}
ComAudio* ComAudio::create()
2013-06-04 17:38:43 +08:00
{
ComAudio * pRet = new (std::nothrow) ComAudio();
2013-06-04 17:38:43 +08:00
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
void ComAudio::end()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::end();
}
void ComAudio::preloadBackgroundMusic(const char* pszFilePath)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(pszFilePath);
2013-12-26 01:17:04 +08:00
setFile(pszFilePath);
setLoop(false);
2013-06-04 17:38:43 +08:00
}
void ComAudio::playBackgroundMusic(const char* pszFilePath, bool loop)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, loop);
2013-12-26 01:17:04 +08:00
2013-06-04 17:38:43 +08:00
}
void ComAudio::playBackgroundMusic(const char* pszFilePath)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath);
2013-06-04 17:38:43 +08:00
}
2013-12-26 01:17:04 +08:00
void ComAudio::playBackgroundMusic()
{
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(_filePath.c_str(), _loop);
}
void ComAudio::stopBackgroundMusic(bool bReleaseData)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(bReleaseData);
2013-06-04 17:38:43 +08:00
}
void ComAudio::stopBackgroundMusic()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
2013-06-04 17:38:43 +08:00
}
void ComAudio::pauseBackgroundMusic()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
2013-06-04 17:38:43 +08:00
}
void ComAudio::resumeBackgroundMusic()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
2013-06-04 17:38:43 +08:00
}
void ComAudio::rewindBackgroundMusic()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->rewindBackgroundMusic();
2013-06-04 17:38:43 +08:00
}
bool ComAudio::willPlayBackgroundMusic()
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->willPlayBackgroundMusic();
2013-06-04 17:38:43 +08:00
}
bool ComAudio::isBackgroundMusicPlaying()
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying();
2013-06-04 17:38:43 +08:00
}
float ComAudio::getBackgroundMusicVolume()
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();
2013-06-04 17:38:43 +08:00
}
void ComAudio::setBackgroundMusicVolume(float volume)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(volume);
2013-06-04 17:38:43 +08:00
}
float ComAudio::getEffectsVolume()
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume();
2013-06-04 17:38:43 +08:00
}
void ComAudio::setEffectsVolume(float volume)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(volume);
2013-06-04 17:38:43 +08:00
}
unsigned int ComAudio::playEffect(const char* pszFilePath, bool loop)
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath, loop);
2013-06-04 17:38:43 +08:00
}
unsigned int ComAudio::playEffect(const char* pszFilePath)
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath);
2013-06-04 17:38:43 +08:00
}
2013-12-26 01:17:04 +08:00
unsigned int ComAudio::playEffect()
{
return CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(_filePath.c_str(), _loop);
}
void ComAudio::pauseEffect(unsigned int nSoundId)
2013-06-04 17:38:43 +08:00
{
return CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(nSoundId);
2013-06-04 17:38:43 +08:00
}
void ComAudio::pauseAllEffects()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->pauseAllEffects();
2013-06-04 17:38:43 +08:00
}
void ComAudio::resumeEffect(unsigned int nSoundId)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->resumeEffect(nSoundId);
2013-06-04 17:38:43 +08:00
}
void ComAudio::resumeAllEffects()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->resumeAllEffects();
2013-06-04 17:38:43 +08:00
}
void ComAudio::stopEffect(unsigned int nSoundId)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(nSoundId);
2013-06-04 17:38:43 +08:00
}
void ComAudio::stopAllEffects()
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
2013-06-04 17:38:43 +08:00
}
void ComAudio::preloadEffect(const char* pszFilePath)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect(pszFilePath);
2013-12-26 01:17:04 +08:00
setFile(pszFilePath);
setLoop(false);
2013-06-04 17:38:43 +08:00
}
void ComAudio::unloadEffect(const char *pszFilePath)
2013-06-04 17:38:43 +08:00
{
CocosDenshion::SimpleAudioEngine::getInstance()->unloadEffect(pszFilePath);
2013-06-04 17:38:43 +08:00
}
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;
}
2013-06-04 17:38:43 +08:00
2016-01-12 17:21:01 +08:00
void ComAudio::start()
{
_startedSoundId = playEffect();
}
void ComAudio::stop()
{
stopEffect(_startedSoundId);
}
}