安卓接入wwise

复制代码
第一步:
#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+")");

        });
    }
};
相关推荐
Dingdangr4 小时前
Android中的Intent的作用
android
技术无疆4 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
GEEKVIP4 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
Jouzzy11 小时前
【Android安全】Ubuntu 16.04安装GDB和GEF
android·ubuntu·gdb
极客先躯11 小时前
java和kotlin 可以同时运行吗
android·java·开发语言·kotlin·同时运行
Good_tea_h14 小时前
Android中的单例模式
android·单例模式
计算机源码社18 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
丶白泽19 小时前
重修设计模式-结构型-门面模式
android
晨春计20 小时前
【git】
android·linux·git
标标大人21 小时前
c语言中的局部跳转以及全局跳转
android·c语言·开发语言