2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "SimpleAudioEngine.h"
|
|
|
|
#include "jni/SimpleAudioEngineJni.h"
|
2012-08-17 09:51:49 +08:00
|
|
|
#include "opensl/SimpleAudioEngineOpenSL.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
#include "cocos2d.h"
|
2012-08-14 10:47:20 +08:00
|
|
|
#include <cstring>
|
2012-08-13 17:14:33 +08:00
|
|
|
#include <android/log.h>
|
|
|
|
#include <jni/JniHelper.h>
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
#define I9100_MODEL "GT-I9100"
|
2012-08-14 10:47:20 +08:00
|
|
|
#define LOG_TAG "Device Model"
|
2012-08-13 17:14:33 +08:00
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
|
|
|
|
|
|
|
static bool s_bI9100 = false;
|
|
|
|
|
2013-01-18 18:05:32 +08:00
|
|
|
USING_NS_CC;
|
2012-08-13 17:14:33 +08:00
|
|
|
/**********************************************************************************
|
|
|
|
* jni
|
|
|
|
**********************************************************************************/
|
2012-09-21 17:01:19 +08:00
|
|
|
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
|
2012-08-13 17:14:33 +08:00
|
|
|
#define METHOD_NAME "getDeviceModel"
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
namespace CocosDenshion {
|
|
|
|
|
2013-01-25 20:51:00 +08:00
|
|
|
static std::string getFullPathWithoutAssetsPrefix(const char* pszFilename)
|
|
|
|
{
|
|
|
|
// Changing file path to full path
|
2013-07-12 12:03:39 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilename);
|
2013-01-25 20:51:00 +08:00
|
|
|
// Removing `assets` since it isn't needed for the API of playing sound.
|
|
|
|
size_t pos = fullPath.find("assets/");
|
|
|
|
if (pos == 0)
|
|
|
|
{
|
|
|
|
fullPath = fullPath.substr(strlen("assets/"));
|
|
|
|
}
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
static SimpleAudioEngine *s_pEngine = 0;
|
|
|
|
|
|
|
|
SimpleAudioEngine::SimpleAudioEngine()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
JniMethodInfo methodInfo;
|
|
|
|
jstring jstr;
|
2013-01-18 18:05:32 +08:00
|
|
|
if (JniHelper::getStaticMethodInfo(methodInfo, CLASS_NAME, METHOD_NAME, "()Ljava/lang/String;"))
|
2012-08-13 17:14:33 +08:00
|
|
|
{
|
|
|
|
jstr = (jstring)methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
|
|
|
|
}
|
|
|
|
methodInfo.env->DeleteLocalRef(methodInfo.classID);
|
|
|
|
|
|
|
|
const char* deviceModel = methodInfo.env->GetStringUTFChars(jstr, NULL);
|
2012-10-16 16:10:14 +08:00
|
|
|
|
2012-08-13 17:14:33 +08:00
|
|
|
LOGD(deviceModel);
|
2012-10-16 16:10:14 +08:00
|
|
|
|
2012-08-14 10:47:20 +08:00
|
|
|
if (strcmp(I9100_MODEL, deviceModel) == 0)
|
2012-08-13 17:14:33 +08:00
|
|
|
{
|
2012-08-17 09:51:49 +08:00
|
|
|
LOGD("i9100 model\nSwitch to OpenSLES");
|
2012-08-13 17:14:33 +08:00
|
|
|
s_bI9100 = true;
|
2013-06-22 04:01:00 +08:00
|
|
|
}
|
2012-10-16 16:10:14 +08:00
|
|
|
|
|
|
|
methodInfo.env->ReleaseStringUTFChars(jstr, deviceModel);
|
|
|
|
methodInfo.env->DeleteLocalRef(jstr);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngine::~SimpleAudioEngine()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->end();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 13:40:45 +08:00
|
|
|
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (! s_pEngine)
|
|
|
|
{
|
|
|
|
s_pEngine = new SimpleAudioEngine();
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_pEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::end()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
endJNI();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);
|
|
|
|
preloadBackgroundMusicJNI(fullPath.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);
|
|
|
|
playBackgroundMusicJNI(fullPath.c_str(), bLoop);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
|
|
|
{
|
|
|
|
stopBackgroundMusicJNI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseBackgroundMusic()
|
|
|
|
{
|
|
|
|
pauseBackgroundMusicJNI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeBackgroundMusic()
|
|
|
|
{
|
|
|
|
resumeBackgroundMusicJNI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::rewindBackgroundMusic()
|
|
|
|
{
|
|
|
|
rewindBackgroundMusicJNI();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
|
|
|
{
|
|
|
|
return isBackgroundMusicPlayingJNI();
|
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getBackgroundMusicVolume()
|
|
|
|
{
|
|
|
|
return getBackgroundMusicVolumeJNI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
|
|
|
{
|
|
|
|
setBackgroundMusicVolumeJNI(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getEffectsVolume()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
return SimpleAudioEngineOpenSL::sharedEngine()->getEffectsVolume();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return getEffectsVolumeJNI();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::setEffectsVolume(float volume)
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->setEffectsVolume(volume);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setEffectsVolumeJNI(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-25 20:51:00 +08:00
|
|
|
std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
2013-06-22 04:01:00 +08:00
|
|
|
return SimpleAudioEngineOpenSL::sharedEngine()->playEffect(fullPath.c_str(), bLoop, pitch, pan, gain);
|
2012-08-13 17:14:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-22 04:01:00 +08:00
|
|
|
return playEffectJNI(fullPath.c_str(), bLoop, pitch, pan, gain);
|
2012-08-13 17:14:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->stopEffect(nSoundId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stopEffectJNI(nSoundId);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);
|
2013-01-18 18:05:32 +08:00
|
|
|
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->preloadEffect(fullPath.c_str());
|
2012-08-13 17:14:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
preloadEffectJNI(fullPath.c_str());
|
2012-08-13 17:14:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);
|
2013-01-18 18:05:32 +08:00
|
|
|
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->unloadEffect(fullPath.c_str());
|
2012-08-13 17:14:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-25 20:51:00 +08:00
|
|
|
unloadEffectJNI(fullPath.c_str());
|
2012-08-13 17:14:33 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->pauseEffect(nSoundId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pauseEffectJNI(nSoundId);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseAllEffects()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->pauseAllEffects();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pauseAllEffectsJNI();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->resumeEffect(nSoundId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resumeEffectJNI(nSoundId);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeAllEffects()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->resumeAllEffects();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resumeAllEffectsJNI();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopAllEffects()
|
|
|
|
{
|
2012-08-13 17:14:33 +08:00
|
|
|
if (s_bI9100)
|
|
|
|
{
|
|
|
|
SimpleAudioEngineOpenSL::sharedEngine()->stopAllEffects();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stopAllEffectsJNI();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|