Adding new methods to Cocos2d-x SimpleAudioEngine

439 views Asked by At

In Cocos2d-x (3.6) I am trying to add increased functionality to SimpleAudioEngine. Specifically, with regard to Android's SoundPool (which SimpleAudioEngine is based upon, at least for Android), I am trying to add some of the methods that have not yet been implemented but are documented in the Android Developer web site.

For example, SoundPool's play method includes a loop variable that is of type int, whereas in SimpleAudioEngine it is of type bool (boolean on the Java side). If I can implement a new method with the loop variable as type int, I will be able to control the number of repetitions directly when I call the playEffect method.

I have figured out most of the changes required, including various C++ header and .cpp files (cocos/audio/include/SimpleAudioEngine.h, cocos/audio/android/cddSimpleAudioEngine.cpp, cocos/audio/android/jni/cddandroidAndroidJavaEngine.h, and cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp) and also the Cocos2dxSound.java file, but I am still getting error messages saying that my new method (which I am calling playEffectN) cannot be found.

The only other place I can see where something might need to be added is in the cocos/audio/openal/SimpleAudioEngineOpenAL.cpp file, but I'm not quite sure how to modify that in order to add my new method.

Can anyone point me in the right direction as to what else I need to do in order to add this additional functionality to SimpleAudioEngine? If I am successful, I will gladly share the results. (If successful, I will also be able to add some of the other unimplemented methods in SoundPool, such as setLoop, setPriority, setRate, and setVolume, for sounds that have already been assigned a soundID.)

1

There are 1 answers

4
yangguang1029 On

first, add c++ apis, in :

cocos/audio/include/AudioEngine.h 

though there is no implement in cddSimpleAudioEngine.cpp, you still need to declare functions in AudioEngine.h

then, the bridge btween c++ and java:

cocos/audio/android/jni/cddandroidAndroidJavaEngine.h
cocos/audio/android/jni/cddandroidAndroidJavaEngine.cpp

here, declare in cddandroidAndroidJavaEngine.h and implement in cddandroidAndroidJavaEngine.cpp, like

 void AndroidJavaEngine::playEffectN(int times) {
        cocos2d::JniMethodInfo methodInfo;

        if (! getJNIStaticMethodInfo(methodInfo, "playEffectN", "(I)V")) {
            return ;
        }

        methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, times);
        methodInfo.env->DeleteLocalRef(methodInfo.classID);
    }

then in java

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java

add function like:

public static void playEffectN(int times) {

}

finally is your java implement, in

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxMusic.java