2013-06-04 17:38:43 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "cocostudio/CCComAudio.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "audio/include/SimpleAudioEngine.h"
|
2014-06-04 14:27:14 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-10-15 18:00:03 +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)
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAudio::ComAudio(void)
|
2013-09-09 19:51:26 +08:00
|
|
|
: _filePath("")
|
|
|
|
, _loop(false)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
_name = "CCComAudio";
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAudio::~ComAudio(void)
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +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();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ComAudio::isEnabled() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _enabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::setEnabled(bool b)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabled = b;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
|
|
|
|
bool ComAudio::serialize(void* r)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(r == nullptr);
|
|
|
|
rapidjson::Value *v = (rapidjson::Value *)r;
|
|
|
|
const char *className = DICTOOL->getStringValue_json(*v, "classname");
|
|
|
|
CC_BREAK_IF(className == nullptr);
|
|
|
|
const char *comName = DICTOOL->getStringValue_json(*v, "name");
|
|
|
|
if (comName != nullptr)
|
|
|
|
{
|
|
|
|
setName(comName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setName(className);
|
|
|
|
}
|
|
|
|
const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData");
|
|
|
|
CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData));
|
|
|
|
const char *file = DICTOOL->getStringValue_json(fileData, "path");
|
|
|
|
CC_BREAK_IF(file == nullptr);
|
|
|
|
std::string filePath;
|
|
|
|
if (file != nullptr)
|
|
|
|
{
|
2014-06-04 14:27:14 +08:00
|
|
|
filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file));
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
int resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1);
|
|
|
|
CC_BREAK_IF(resType != 0);
|
|
|
|
if (strcmp(className, "CCBackgroundAudio") == 0)
|
|
|
|
{
|
2014-03-22 21:13:02 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
|
|
|
// no MP3 support for CC_PLATFORM_WP8
|
|
|
|
std::string::size_type pos = filePath.find(".mp3");
|
|
|
|
if (pos == filePath.npos)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
filePath.replace(pos, filePath.length(), ".wav");
|
|
|
|
#endif
|
2014-01-05 02:22:32 +08:00
|
|
|
preloadBackgroundMusic(filePath.c_str());
|
|
|
|
bool loop = DICTOOL->getIntValue_json(*v, "loop") != 0? true:false;
|
|
|
|
setLoop(loop);
|
|
|
|
playBackgroundMusic(filePath.c_str(), loop);
|
|
|
|
}
|
|
|
|
else if(strcmp(className, "CCComAudio") == 0)
|
|
|
|
{
|
|
|
|
preloadEffect(filePath.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(true);
|
|
|
|
}
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAudio* ComAudio::create(void)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ComAudio * pRet = new ComAudio();
|
2013-06-04 17:38:43 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::end()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CocosDenshion::SimpleAudioEngine::end();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::preloadBackgroundMusic(const char* pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
void ComAudio::playBackgroundMusic(const char* pszFilePath, bool loop)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2014-01-05 02:22:32 +08:00
|
|
|
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(pszFilePath, loop);
|
2013-12-26 01:17:04 +08:00
|
|
|
|
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
|
|
|
{
|
2013-07-19 07:30:19 +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);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::stopBackgroundMusic(bool bReleaseData)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +08:00
|
|
|
return 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
|
|
|
{
|
2013-07-19 07:30:19 +08:00
|
|
|
return 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
|
|
|
{
|
2013-07-19 07:30:19 +08:00
|
|
|
return 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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +08:00
|
|
|
return 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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2014-01-05 02:22:32 +08:00
|
|
|
return 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
|
|
|
{
|
2013-07-19 07:30:19 +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);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::pauseEffect(unsigned int nSoundId)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-19 07:30:19 +08:00
|
|
|
return 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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
{
|
2013-07-19 07:30:19 +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
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ComAudio::unloadEffect(const char *pszFilePath)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-07-19 07:30:19 +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)
|
|
|
|
{
|
|
|
|
_filePath.assign(pszFilePath);
|
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
void ComAudio::setLoop(bool loop)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-05 02:22:32 +08:00
|
|
|
_loop = loop;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* ComAudio::getFile()
|
|
|
|
{
|
|
|
|
return _filePath.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComAudio::isLoop()
|
|
|
|
{
|
|
|
|
return _loop;
|
|
|
|
}
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|