2011-01-19 16:00:19 +08:00
|
|
|
#include "SimpleAudioEngineJni.h"
|
|
|
|
#include <android/log.h>
|
|
|
|
|
2011-01-21 15:15:09 +08:00
|
|
|
#define LOG_TAG "libSimpleAudioEngine"
|
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
|
|
|
|
2011-01-19 16:00:19 +08:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2011-06-25 11:21:16 +08:00
|
|
|
static JavaVM *gJavaVM = 0;
|
2011-01-19 16:00:19 +08:00
|
|
|
static jclass classOfCocos2dxActivity = 0;
|
|
|
|
JNIEnv *env = 0;
|
|
|
|
|
2011-06-25 11:21:16 +08:00
|
|
|
jint JNI_OnLoad(JavaVM *vm, void *reserved)
|
|
|
|
{
|
|
|
|
gJavaVM = vm;
|
|
|
|
|
|
|
|
return JNI_VERSION_1_4;
|
|
|
|
}
|
|
|
|
|
2011-01-19 16:00:19 +08:00
|
|
|
static jmethodID getMethodID(const char *methodName, const char *paramCode)
|
|
|
|
{
|
|
|
|
jmethodID ret = 0;
|
|
|
|
|
|
|
|
// get jni environment and java class for Cocos2dxActivity
|
|
|
|
if (gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
|
|
|
|
{
|
2011-01-21 15:15:09 +08:00
|
|
|
LOGD("Failed to get the environment using GetEnv()");
|
2011-01-19 16:00:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gJavaVM->AttachCurrentThread(&env, 0) < 0)
|
|
|
|
{
|
2011-01-21 15:15:09 +08:00
|
|
|
LOGD("Failed to get the environment using AttachCurrentThread()");
|
2011-01-19 16:00:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
classOfCocos2dxActivity = env->FindClass("org/cocos2dx/lib/Cocos2dxActivity");
|
|
|
|
if (! classOfCocos2dxActivity)
|
|
|
|
{
|
2011-01-21 15:15:09 +08:00
|
|
|
LOGD("Failed to find class of org/cocos2dx/lib/Cocos2dxActivity");
|
2011-01-19 16:00:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env != 0 && classOfCocos2dxActivity != 0)
|
|
|
|
{
|
|
|
|
ret = env->GetStaticMethodID(classOfCocos2dxActivity, methodName, paramCode);
|
2011-09-20 14:25:16 +08:00
|
|
|
env->DeleteLocalRef(classOfCocos2dxActivity);
|
2011-01-19 16:00:19 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:15:09 +08:00
|
|
|
if (! ret)
|
2011-01-19 16:00:19 +08:00
|
|
|
{
|
2011-01-21 15:15:09 +08:00
|
|
|
LOGD("get method id of %s error", methodName);
|
2011-01-19 16:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-28 10:24:52 +08:00
|
|
|
void preloadBackgroundMusicJNI(const char *path)
|
|
|
|
{
|
|
|
|
// void playBackgroundMusic(String,boolean)
|
|
|
|
jmethodID preloadBackgroundMusicMethodID = getMethodID("preloadBackgroundMusic", "(Ljava/lang/String;)V");
|
|
|
|
|
|
|
|
if (preloadBackgroundMusicMethodID)
|
|
|
|
{
|
2011-09-20 14:25:16 +08:00
|
|
|
jstring stringArg = env->NewStringUTF(path);
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadBackgroundMusicMethodID, stringArg);
|
|
|
|
env->DeleteLocalRef(stringArg);
|
2011-08-28 10:24:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-19 16:00:19 +08:00
|
|
|
void playBackgroundMusicJNI(const char *path, bool isLoop)
|
|
|
|
{
|
|
|
|
// void playBackgroundMusic(String,boolean)
|
|
|
|
jmethodID playBackgroundMusicMethodID = getMethodID("playBackgroundMusic", "(Ljava/lang/String;Z)V");
|
|
|
|
|
|
|
|
if (playBackgroundMusicMethodID)
|
|
|
|
{
|
2011-09-20 14:25:16 +08:00
|
|
|
jstring stringArg = env->NewStringUTF(path);
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, playBackgroundMusicMethodID, stringArg, isLoop);
|
|
|
|
env->DeleteLocalRef(stringArg);
|
2011-01-19 16:00:19 +08:00
|
|
|
}
|
|
|
|
}
|
2011-01-21 15:15:09 +08:00
|
|
|
|
|
|
|
void stopBackgroundMusicJNI()
|
|
|
|
{
|
|
|
|
// void stopBackgroundMusic()
|
|
|
|
jmethodID stopBackgroundMusicMethodID = getMethodID("stopBackgroundMusic", "()V");
|
|
|
|
|
|
|
|
if (stopBackgroundMusicMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, stopBackgroundMusicMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pauseBackgroundMusicJNI()
|
|
|
|
{
|
|
|
|
// void pauseBackgroundMusic()
|
|
|
|
jmethodID pauseBackgroundMusicMethodID = getMethodID("pauseBackgroundMusic", "()V");
|
|
|
|
|
|
|
|
if (pauseBackgroundMusicMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, pauseBackgroundMusicMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resumeBackgroundMusicJNI()
|
|
|
|
{
|
|
|
|
// void resumeBackgroundMusic()
|
|
|
|
jmethodID resumeBackgroundMusicMethodID = getMethodID("resumeBackgroundMusic", "()V");
|
|
|
|
|
|
|
|
if (resumeBackgroundMusicMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, resumeBackgroundMusicMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewindBackgroundMusicJNI()
|
|
|
|
{
|
|
|
|
// void rewindBackgroundMusic()
|
|
|
|
jmethodID rewindBackgroundMusicMethodID = getMethodID("rewindBackgroundMusic", "()V");
|
|
|
|
|
|
|
|
if (rewindBackgroundMusicMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, rewindBackgroundMusicMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBackgroundMusicPlayingJNI()
|
|
|
|
{
|
|
|
|
// boolean rewindBackgroundMusic()
|
|
|
|
jmethodID isBackgroundMusicPlayingMethodID = getMethodID("isBackgroundMusicPlaying", "()Z");
|
|
|
|
jboolean ret = false;
|
|
|
|
|
|
|
|
if (isBackgroundMusicPlayingMethodID)
|
|
|
|
{
|
|
|
|
ret = env->CallStaticBooleanMethod(classOfCocos2dxActivity, isBackgroundMusicPlayingMethodID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getBackgroundMusicVolumeJNI()
|
|
|
|
{
|
|
|
|
// float getBackgroundMusicVolume()
|
|
|
|
jmethodID getBackgroundMusicVolumeID = getMethodID("getBackgroundMusicVolume", "()F");
|
|
|
|
jfloat ret = 0.0;
|
|
|
|
|
|
|
|
if (getBackgroundMusicVolumeID)
|
|
|
|
{
|
|
|
|
ret = env->CallStaticFloatMethod(classOfCocos2dxActivity, getBackgroundMusicVolumeID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBackgroundMusicVolumeJNI(float volume)
|
|
|
|
{
|
|
|
|
// void setBackgroundMusicVolume()
|
|
|
|
jmethodID setBackgroundMusicVolumeMethodID = getMethodID("setBackgroundMusicVolume", "(F)V");
|
|
|
|
|
|
|
|
if (setBackgroundMusicVolumeMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, setBackgroundMusicVolumeMethodID, volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 15:52:41 +08:00
|
|
|
unsigned int playEffectJNI(const char* path, bool bLoop)
|
2011-01-21 15:15:09 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
// int playEffect(String)
|
2011-07-26 15:52:41 +08:00
|
|
|
jmethodID playEffectMethodID = getMethodID("playEffect", "(Ljava/lang/String;Z)I");
|
2011-01-21 15:15:09 +08:00
|
|
|
|
|
|
|
if (playEffectMethodID)
|
|
|
|
{
|
2011-09-20 14:25:16 +08:00
|
|
|
jstring stringArg = env->NewStringUTF(path);
|
|
|
|
ret = env->CallStaticIntMethod(classOfCocos2dxActivity, playEffectMethodID, stringArg, bLoop);
|
|
|
|
env->DeleteLocalRef(stringArg);
|
2011-01-21 15:15:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned int)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopEffectJNI(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
// void stopEffect(int)
|
|
|
|
jmethodID stopEffectMethodID = getMethodID("stopEffect", "(I)V");
|
|
|
|
|
|
|
|
if (stopEffectMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, stopEffectMethodID, (int)nSoundId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void endJNI()
|
|
|
|
{
|
|
|
|
// void end()
|
|
|
|
jmethodID endMethodID = getMethodID("end", "()V");
|
|
|
|
|
|
|
|
if (endMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, endMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float getEffectsVolumeJNI()
|
|
|
|
{
|
|
|
|
// float getEffectsVolume()
|
|
|
|
jmethodID getEffectsVolumeMethodID = getMethodID("getEffectsVolume", "()F");
|
|
|
|
jfloat ret = -1.0;
|
|
|
|
|
|
|
|
if (getEffectsVolumeMethodID)
|
|
|
|
{
|
|
|
|
ret = env->CallStaticFloatMethod(classOfCocos2dxActivity, getEffectsVolumeMethodID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setEffectsVolumeJNI(float volume)
|
|
|
|
{
|
|
|
|
// void setEffectsVolume(float)
|
|
|
|
jmethodID setEffectsVolumeMethodID = getMethodID("setEffectsVolume", "(F)V");
|
|
|
|
|
|
|
|
if (setEffectsVolumeMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, setEffectsVolumeMethodID, volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void preloadEffectJNI(const char *path)
|
|
|
|
{
|
|
|
|
// void preloadEffect(String)
|
|
|
|
jmethodID preloadEffectMethodID = getMethodID("preloadEffect", "(Ljava/lang/String;)V");
|
|
|
|
|
|
|
|
if (preloadEffectMethodID)
|
|
|
|
{
|
2011-09-20 14:25:16 +08:00
|
|
|
jstring stringArg = env->NewStringUTF(path);
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, preloadEffectMethodID, stringArg);
|
|
|
|
env->DeleteLocalRef(stringArg);
|
2011-01-21 15:15:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unloadEffectJNI(const char* path)
|
|
|
|
{
|
|
|
|
// void unloadEffect(String)
|
|
|
|
jmethodID unloadEffectMethodID = getMethodID("unloadEffect", "(Ljava/lang/String;)V");
|
|
|
|
|
|
|
|
if (unloadEffectMethodID)
|
|
|
|
{
|
2011-09-20 14:25:16 +08:00
|
|
|
jstring stringArg = env->NewStringUTF(path);
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, unloadEffectMethodID, stringArg);
|
|
|
|
env->DeleteLocalRef(stringArg);
|
2011-01-21 15:15:09 +08:00
|
|
|
}
|
|
|
|
}
|
2011-11-25 09:56:06 +08:00
|
|
|
|
|
|
|
void pauseEffectJNI(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
// void pauseEffect(int)
|
|
|
|
jmethodID pauseEffectMethodID = getMethodID("pauseEffect", "(I)V");
|
|
|
|
|
|
|
|
if (pauseEffectMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, pauseEffectMethodID, (int)nSoundId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pauseAllEffectsJNI()
|
|
|
|
{
|
|
|
|
// void pauseAllEffects()
|
|
|
|
jmethodID pauseAllEffectsMethodID = getMethodID("pauseAllEffects", "()V");
|
|
|
|
|
|
|
|
if (pauseAllEffectsMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, pauseAllEffectsMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resumeEffectJNI(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
// void resumeEffect(int)
|
|
|
|
jmethodID resumeEffectMethodID = getMethodID("resumeEffect", "(I)V");
|
|
|
|
|
|
|
|
if (resumeEffectMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, resumeEffectMethodID, (int)nSoundId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resumeAllEffectsJNI()
|
|
|
|
{
|
|
|
|
// void resumeAllEffects()
|
|
|
|
jmethodID resumeAllEffectsMethodID = getMethodID("resumeAllEffects", "()V");
|
|
|
|
|
|
|
|
if (resumeAllEffectsMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, resumeAllEffectsMethodID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopAllEffectsJNI()
|
|
|
|
{
|
|
|
|
// void stopAllEffects()
|
|
|
|
jmethodID stopAllEffectsMethodID = getMethodID("stopAllEffects", "()V");
|
|
|
|
|
|
|
|
if (stopAllEffectsMethodID)
|
|
|
|
{
|
|
|
|
env->CallStaticVoidMethod(classOfCocos2dxActivity, stopAllEffectsMethodID);
|
|
|
|
}
|
|
|
|
}
|
2011-01-19 16:00:19 +08:00
|
|
|
}
|