2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2019-11-23 20:27:39 +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-17 16:32:16 +08:00
|
|
|
#include "libeditor/CCComAudio.h"
|
|
|
|
// #include "audio/include/SimpleAudioEngine.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// TODO: use audio engine to play
|
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic(pszFilePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
setFile(pszFilePath);
|
|
|
|
setLoop(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::playBackgroundMusic(const char* pszFilePath, bool loop)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, loop);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::playBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::playBackgroundMusic()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(_filePath.c_str(), _loop);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::stopBackgroundMusic(bool bReleaseData)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(bReleaseData);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::stopBackgroundMusic()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::pauseBackgroundMusic()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::resumeBackgroundMusic()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::rewindBackgroundMusic()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->rewindBackgroundMusic();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ComAudio::willPlayBackgroundMusic()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return false; // CocosDenshion::SimpleAudioEngine::getInstance()->willPlayBackgroundMusic();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ComAudio::isBackgroundMusicPlaying()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return false;// CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float ComAudio::getBackgroundMusicVolume()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->getBackgroundMusicVolume();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::setBackgroundMusicVolume(float volume)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(volume);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float ComAudio::getEffectsVolume()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::setEffectsVolume(float volume)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->setEffectsVolume(volume);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ComAudio::playEffect(const char* pszFilePath, bool loop)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath, loop);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ComAudio::playEffect(const char* pszFilePath)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ComAudio::playEffect()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(_filePath.c_str(), _loop);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::pauseEffect(unsigned int nSoundId)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->pauseEffect(nSoundId);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::pauseAllEffects()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->pauseAllEffects();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::resumeEffect(unsigned int nSoundId)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->resumeEffect(nSoundId);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::resumeAllEffects()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->resumeAllEffects();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::stopEffect(unsigned int nSoundId)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopEffect(nSoundId);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::stopAllEffects()
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect(pszFilePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
setFile(pszFilePath);
|
|
|
|
setLoop(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::unloadEffect(const char *pszFilePath)
|
|
|
|
{
|
2019-11-25 01:35:26 +08:00
|
|
|
// CocosDenshion::SimpleAudioEngine::getInstance()->unloadEffect(pszFilePath);
|
2019-11-23 20:27:39 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::start()
|
|
|
|
{
|
|
|
|
_startedSoundId = playEffect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComAudio::stop()
|
|
|
|
{
|
|
|
|
stopEffect(_startedSoundId);
|
|
|
|
}
|
|
|
|
}
|