安卓接入wwise

发布于:2024-05-02 ⋅ 阅读:(21) ⋅ 点赞:(0)
第一步:
#include "com_hs_androidjnidemo_MainActivity.h"
#include "jni.h"
#include <stdio.h>
#include <YLWwiseEngine.h>
#include <AK/SoundEngine/Common/AkTypes.h>

//全局变量 gaden
JavaVM *g_vm;

//0.JNI_OnLoad
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    g_vm = vm;
    return JNI_VERSION_1_6;
}

//1.initWwise
JNIEXPORT void JNICALL Java_com_hs_androidjnidemo_MainActivity_initWwise (JNIEnv *env, jobject thiz, jobject activity,jstring path) {
    const char* cStr = env->GetStringUTFChars(path, nullptr);
    YLWwiseEngine::getInstance()->init(cStr,env,thiz,activity,g_vm);
   // return env->NewStringUTF("string1 from c");
}

//2.SetBasePath
JNIEXPORT void JNICALL Java_com_hs_androidjnidemo_MainActivity_SetBasePath (JNIEnv *env, jobject thiz,jstring path) {
    const char* cStr = env->GetStringUTFChars(path, nullptr);
    YLWwiseEngine::getInstance()->SetBasePath(cStr);
}

第二步:

bool YLWwiseEngine::init(const char* basePath,JNIEnv *env,jobject obj,jobject activity,JavaVM *g_vm)
{
       this->env1 =env;
       this->g_VM2=g_vm;
       AkPlatformInitSettings platformInitSettings;
       
       env->GetJavaVM(&g_VM1);

       jobject mainActivityObj = env->NewGlobalRef(activity);
       platformInitSettings.pJavaVM = g_VM1;
       platformInitSettings.jActivity =mainActivityObj;

       this->activity1 =mainActivityObj; //注意activity1 

       AKRESULT res0 = m_pLowLevelIO->Init(platformInitSettings.pJavaVM,platformInitSettings.jActivity);
        
         if (res0 != AK_Success)
            {
                __android_log_print(ANDROID_LOG_INFO, "lclclc", "---Error----\n");
                return false;
            }

第三步:SetBasePath

void YLWwiseEngine::SetBasePath(const char* basePath)
{
    char* basePaeht1 = new char[1024];
    strcpy(basePaeht1,basePath);
    AKRESULT ret = m_pLowLevelIO->SetBasePath(basePaeht1);
    if (ret != AK_Success)
    {
         __android_log_print(ANDROID_LOG_INFO, "lclclc", "-------SetBasePath22--------         ret %d\n", ret); //log i类型
    }
}

第四步:postEventFun回调

int YLWwiseEngine::postEventFun(const char* eventName, unsigned int gameObjectId, unsigned int funId,JNIEnv *env,JavaVM *g_vm)
{
    this->env1 =env;
    char* pcname = new char[1024];//锟姐够锟斤拷
    strcpy(pcname, eventName);

    PostObj* postobj = new PostObj();
    postobj->funId = funId;
    postobj->eventName = pcname;
     postobj->env = env;
     postobj->g_vm = g_vm;
    AkPlayingID  id = AK::SoundEngine::PostEvent(eventName, gameObjectId,
                                                 AK_EndOfEvent | AK_Marker | AK_EnableGetSourcePlayPosition,
                                                 PostEventGan::getInstance()->postCallback, postobj);
  //  this->update();
    AK::SoundEngine::RenderAudio();
    if (id == AK_INVALID_PLAYING_ID)
    {
        printf("postEventʧ�ܣ���������%d\n", id);
        return 0;
    }
    return id;
}

回调

void PostEventGan::postCallback(AkCallbackType in_eType, AkCallbackInfo* in_pCallbackInfo)
{
   PostObj* curPost1 = (PostObj*)in_pCallbackInfo->pCookie;
   if (in_eType == AK_EndOfEvent)
   {

//    char* geline = "|";
      int funId = curPost1->funId;
      char funId_str[10];
      sprintf(funId_str,"%d",funId);
      __android_log_print(ANDROID_LOG_INFO, "lclclc", "------PostEventGan::postCallback2---111----funId_str %s\n", funId_str); //log i类型
      //ccjsbridge::excuteJS("CCToJsUtils", "CccToJSAK_EndOfEvent", funId_str);

            JavaVM *g_vm=curPost1->g_vm;
            JNIEnv *env1 = nullptr;
            jint result = g_vm->AttachCurrentThread(&env1, 0);
           jclass mainActivityClass = env1->GetObjectClass(YLWwiseEngine::getInstance()->activity1);
           const char *sig = "(Ljava/lang/String;)V";//"(F)V"
           jmethodID mainMethodId = env1->GetMethodID(mainActivityClass, "postEventFunOver", sig);
           env1->CallVoidMethod(YLWwiseEngine::getInstance()->activity1, mainMethodId,env1->NewStringUTF(funId_str));
            g_vm->DetachCurrentThread();
   }

第五步:子线程回到 java 里面UI线程

public void postEventFunOver(String para) {
    if (Looper.getMainLooper() == Looper.myLooper()) {
        System.out.print("-------NativeHelper--c++返回值------Looper.getMainLooper():"+Looper.getMainLooper()+"\n");
    } else {
        System.out.print("-------NativeHelper--c++返回值------a:"+para+"\n");
        activity.runOnUiThread(()-> {
            // activity.getJsInterface().youLoftLoginFail();
            activity.getWebView().loadUrl("javascript:wwise_CccToJSAK_EndOfEvent("+para+")");

        });
    }
};