2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "audio/include/SimpleAudioEngine.h"
|
2016-03-21 11:02:00 +08:00
|
|
|
#include "audio/ios/SimpleAudioEngine_objc.h"
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
USING_NS_CC;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2017-03-13 11:37:23 +08:00
|
|
|
static bool __isAudioPreloadOrPlayed = false;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
static void static_end()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (__isAudioPreloadOrPlayed)
|
|
|
|
{
|
|
|
|
[SimpleAudioEngine end];
|
|
|
|
}
|
|
|
|
|
|
|
|
__isAudioPreloadOrPlayed = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void static_preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
__isAudioPreloadOrPlayed = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic: [NSString stringWithUTF8String: pszFilePath]];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
__isAudioPreloadOrPlayed = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] playBackgroundMusic: [NSString stringWithUTF8String: pszFilePath] loop: bLoop];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_stopBackgroundMusic()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_pauseBackgroundMusic()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_resumeBackgroundMusic()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_rewindBackgroundMusic()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] rewindBackgroundMusic];
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool static_willPlayBackgroundMusic()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return false;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return [[SimpleAudioEngine sharedEngine] willPlayBackgroundMusic];
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool static_isBackgroundMusicPlaying()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return false;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return [[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying];
|
|
|
|
}
|
|
|
|
|
|
|
|
static float static_getBackgroundMusicVolume()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return 0.0f;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return [[SimpleAudioEngine sharedEngine] backgroundMusicVolume];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_setBackgroundMusicVolume(float volume)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
volume = MAX( MIN(volume, 1.0), 0 );
|
|
|
|
[SimpleAudioEngine sharedEngine].backgroundMusicVolume = volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float static_getEffectsVolume()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return 0.0f;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return [[SimpleAudioEngine sharedEngine] effectsVolume];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_setEffectsVolume(float volume)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
volume = MAX( MIN(volume, 1.0), 0 );
|
|
|
|
[SimpleAudioEngine sharedEngine].effectsVolume = volume;
|
|
|
|
}
|
2013-06-03 20:11:00 +08:00
|
|
|
|
|
|
|
static unsigned int static_playEffect(const char* pszFilePath, bool bLoop, Float32 pszPitch, Float32 pszPan, Float32 pszGain)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
__isAudioPreloadOrPlayed = true;
|
2013-06-03 20:11:00 +08:00
|
|
|
return [[SimpleAudioEngine sharedEngine] playEffect:[NSString stringWithUTF8String: pszFilePath] loop:bLoop pitch:pszPitch pan: pszPan gain:pszGain];
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void static_stopEffect(int nSoundId)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] stopEffect: nSoundId];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
__isAudioPreloadOrPlayed = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] preloadEffect: [NSString stringWithUTF8String: pszFilePath]];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_unloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] unloadEffect: [NSString stringWithUTF8String: pszFilePath]];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_pauseEffect(unsigned int uSoundId)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] pauseEffect: uSoundId];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_pauseAllEffects()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] pauseAllEffects];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_resumeEffect(unsigned int uSoundId)
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] resumeEffect: uSoundId];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_resumeAllEffects()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] resumeAllEffects];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void static_stopAllEffects()
|
|
|
|
{
|
2017-03-13 11:37:23 +08:00
|
|
|
if (!__isAudioPreloadOrPlayed)
|
|
|
|
return;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
[[SimpleAudioEngine sharedEngine] stopAllEffects];
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace CocosDenshion {
|
|
|
|
|
2017-03-13 11:37:23 +08:00
|
|
|
static SimpleAudioEngine *s_pEngine = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
SimpleAudioEngine::SimpleAudioEngine()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngine::~SimpleAudioEngine()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (! s_pEngine)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
s_pEngine = new (std::nothrow) SimpleAudioEngine();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_pEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::end()
|
|
|
|
{
|
|
|
|
if (s_pEngine)
|
|
|
|
{
|
|
|
|
delete s_pEngine;
|
2016-06-23 11:39:23 +08:00
|
|
|
s_pEngine = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-06-21 11:42:49 +08:00
|
|
|
static_end();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
// Changing file path to full path
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-01-25 20:51:52 +08:00
|
|
|
static_preloadBackgroundMusic(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
// Changing file path to full path
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-01-25 20:51:52 +08:00
|
|
|
static_playBackgroundMusic(fullPath.c_str(), bLoop);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
static_stopBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseBackgroundMusic()
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
static_pauseBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeBackgroundMusic()
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
static_resumeBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::rewindBackgroundMusic()
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
static_rewindBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
return static_willPlayBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
return static_isBackgroundMusicPlaying();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getBackgroundMusicVolume()
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
return static_getBackgroundMusicVolume();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
|
|
|
{
|
2012-06-21 11:42:49 +08:00
|
|
|
static_setBackgroundMusicVolume(volume);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getEffectsVolume()
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
return static_getEffectsVolume();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::setEffectsVolume(float volume)
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
static_setEffectsVolume(volume);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-03 20:11:00 +08:00
|
|
|
unsigned int SimpleAudioEngine::playEffect(const char *pszFilePath, bool bLoop,
|
|
|
|
float pitch, float pan, float gain)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
// Changing file path to full path
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-06-03 20:11:00 +08:00
|
|
|
return static_playEffect(fullPath.c_str(), bLoop, pitch, pan, gain);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
static_stopEffect(nSoundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
// Changing file path to full path
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-01-25 20:51:52 +08:00
|
|
|
static_preloadEffect(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2013-01-18 18:05:32 +08:00
|
|
|
// Changing file path to full path
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-01-25 20:51:52 +08:00
|
|
|
static_unloadEffect(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseEffect(unsigned int uSoundId)
|
|
|
|
{
|
|
|
|
static_pauseEffect(uSoundId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeEffect(unsigned int uSoundId)
|
|
|
|
{
|
|
|
|
static_resumeEffect(uSoundId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseAllEffects()
|
|
|
|
{
|
|
|
|
static_pauseAllEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeAllEffects()
|
|
|
|
{
|
|
|
|
static_resumeAllEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopAllEffects()
|
|
|
|
{
|
|
|
|
static_stopAllEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // endof namespace CocosDenshion {
|