2013-06-04 17:38:43 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCComAudio.h"
|
2014-06-04 14:27:14 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
IMPLEMENT_CLASS_COMPONENT_INFO(ComAudio)
|
2015-09-18 11:48:43 +08:00
|
|
|
|
|
|
|
const std::string ComAudio::COMPONENT_NAME = "CCComAudio";
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ComAudio::ComAudio() : _filePath(""), _loop(false), _startedSoundId(0)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
_name = COMPONENT_NAME;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ComAudio::~ComAudio() {}
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ComAudio::init()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ComAudio::onEnter() {}
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::onExit()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
stopBackgroundMusic(true);
|
|
|
|
stopAllEffects();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ComAudio::onAdd() {}
|
2015-06-04 14:06:31 +08:00
|
|
|
|
|
|
|
void ComAudio::onRemove()
|
|
|
|
{
|
|
|
|
stopBackgroundMusic(true);
|
|
|
|
stopAllEffects();
|
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
bool ComAudio::serialize(void* r)
|
|
|
|
{
|
2014-06-30 17:15:55 +08:00
|
|
|
bool ret = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-06-25 18:10:06 +08:00
|
|
|
if (strcmp(file, "") == 0)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
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;
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
ComAudio* ComAudio::create()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ComAudio* pRet = new ComAudio();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (pRet->init())
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return pRet;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ComAudio::end() {}
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::preloadBackgroundMusic(const char* pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// TODO: use audio engine to play
|
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(pszFilePath);
|
2013-12-26 01:17:04 +08:00
|
|
|
setFile(pszFilePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
setLoop(false);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
void ComAudio::playBackgroundMusic(const char* pszFilePath, bool loop)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, loop);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::playBackgroundMusic(const char* pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +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()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(_filePath.c_str(), _loop);
|
2013-12-26 01:17:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::stopBackgroundMusic(bool bReleaseData)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(bReleaseData);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::stopBackgroundMusic()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::pauseBackgroundMusic()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::resumeBackgroundMusic()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::rewindBackgroundMusic()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->rewindBackgroundMusic();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ComAudio::willPlayBackgroundMusic()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return false; // CocosDenshion::SimpleAudioEngine::getInstance()->willPlayBackgroundMusic();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ComAudio::isBackgroundMusicPlaying()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return false; // CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ComAudio::getBackgroundMusicVolume()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::setBackgroundMusicVolume(float volume)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(volume);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float ComAudio::getEffectsVolume()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::setEffectsVolume(float volume)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(volume);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
unsigned int ComAudio::playEffect(const char* pszFilePath, bool loop)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath, loop);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
unsigned int ComAudio::playEffect(const char* pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0; // 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()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(_filePath.c_str(), _loop);
|
2013-12-26 01:17:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::pauseEffect(unsigned int nSoundId)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(nSoundId);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::pauseAllEffects()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->pauseAllEffects();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::resumeEffect(unsigned int nSoundId)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->resumeEffect(nSoundId);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::resumeAllEffects()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->resumeAllEffects();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::stopEffect(unsigned int nSoundId)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(nSoundId);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::stopAllEffects()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::preloadEffect(const char* pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +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
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ComAudio::unloadEffect(const char* pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->unloadEffect(pszFilePath);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-09 19:51:26 +08:00
|
|
|
void ComAudio::setFile(const char* pszFilePath)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_filePath.assign(pszFilePath);
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
void ComAudio::setLoop(bool loop)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_loop = loop;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* ComAudio::getFile()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return _filePath.c_str();
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ComAudio::isLoop()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return _loop;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
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);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|