axmol/extensions/cocostudio/CCComAudio.cpp

322 lines
8.8 KiB
C++
Raw Normal View History

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
2022-02-24 18:51:36 +08:00
https://adxeproject.github.io/
2019-11-23 20:27:39 +08:00
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 "CCComAudio.h"
2019-11-23 20:27:39 +08:00
#include "platform/CCFileUtils.h"
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
IMPLEMENT_CLASS_COMPONENT_INFO(ComAudio)
const std::string ComAudio::COMPONENT_NAME = "CCComAudio";
2021-12-25 10:04:45 +08:00
ComAudio::ComAudio() : _filePath(""), _loop(false), _startedSoundId(0)
2019-11-23 20:27:39 +08:00
{
_name = COMPONENT_NAME;
}
2021-12-25 10:04:45 +08:00
ComAudio::~ComAudio() {}
2019-11-23 20:27:39 +08:00
bool ComAudio::init()
{
return true;
}
2021-12-25 10:04:45 +08:00
void ComAudio::onEnter() {}
2019-11-23 20:27:39 +08:00
void ComAudio::onExit()
{
stopBackgroundMusic(true);
stopAllEffects();
}
2021-12-25 10:04:45 +08:00
void ComAudio::onAdd() {}
2019-11-23 20:27:39 +08:00
void ComAudio::onRemove()
{
stopBackgroundMusic(true);
stopAllEffects();
}
bool ComAudio::serialize(void* r)
{
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)
{
2019-11-23 20:27:39 +08:00
if (strcmp(file, "") == 0)
{
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;
2019-11-23 20:27:39 +08:00
}
ComAudio* ComAudio::create()
{
2021-12-25 10:04:45 +08:00
ComAudio* pRet = new ComAudio();
2021-12-08 00:11:53 +08:00
if (pRet->init())
2019-11-23 20:27:39 +08:00
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
2021-12-25 10:04:45 +08:00
return pRet;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void ComAudio::end() {}
2019-11-23 20:27:39 +08:00
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);
2021-12-25 10:04:45 +08:00
setLoop(false);
2019-11-23 20:27:39 +08:00
}
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()
{
2021-12-25 10:04:45 +08:00
return false; // CocosDenshion::SimpleAudioEngine::getInstance()->willPlayBackgroundMusic();
2019-11-23 20:27:39 +08:00
}
bool ComAudio::isBackgroundMusicPlaying()
{
2021-12-25 10:04:45 +08:00
return false; // CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying();
2019-11-23 20:27:39 +08:00
}
float ComAudio::getBackgroundMusicVolume()
{
2021-12-25 10:04:45 +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()
{
2021-12-25 10:04:45 +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)
{
2021-12-25 10:04:45 +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)
{
2021-12-25 10:04:45 +08:00
return 0; // CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(pszFilePath);
2019-11-23 20:27:39 +08:00
}
unsigned int ComAudio::playEffect()
{
2021-12-25 10:04:45 +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);
}
2021-12-25 10:04:45 +08:00
void ComAudio::unloadEffect(const char* pszFilePath)
2019-11-23 20:27:39 +08:00
{
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)
{
2021-12-25 10:04:45 +08:00
_filePath.assign(pszFilePath);
2019-11-23 20:27:39 +08:00
}
void ComAudio::setLoop(bool loop)
{
2021-12-25 10:04:45 +08:00
_loop = loop;
2019-11-23 20:27:39 +08:00
}
const char* ComAudio::getFile()
{
2021-12-25 10:04:45 +08:00
return _filePath.c_str();
2019-11-23 20:27:39 +08:00
}
bool ComAudio::isLoop()
{
2021-12-25 10:04:45 +08:00
return _loop;
2019-11-23 20:27:39 +08:00
}
void ComAudio::start()
{
_startedSoundId = playEffect();
}
void ComAudio::stop()
{
stopEffect(_startedSoundId);
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio