安卓接入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+")");

        });
    }
};
相关推荐
lkbhua莱克瓦24几秒前
基础-约束
android·开发语言·数据库·笔记·sql·mysql·约束
戴西软件13 分钟前
CAxWorks.VPG车辆工程仿真软件:打造新能源汽车安全的“数字防线“
android·大数据·运维·人工智能·安全·低代码·汽车
ljt272496066116 分钟前
Compose笔记(六十三)--SegmentedButton
android·笔记·android jetpack
Android系统攻城狮17 分钟前
Android ALSA驱动进阶之设置共享内存snd_pcm_lib_mmap_iomem:用法实例(九十九)
android·pcm·音频进阶·alsa驱动·android驱动
ZHANG13HAO25 分钟前
Android 13 系统源码定制的保活方案,
android
2501_946230989 小时前
Cordova&OpenHarmony通知中心实现
android·javascript
csj5010 小时前
安卓基础之《(9)—中级控件(3)文本输入》
android
Elastic 中国社区官方博客11 小时前
Elasticsearch:在 X-mas 吃一些更健康的东西
android·大数据·数据库·人工智能·elasticsearch·搜索引擎·全文检索
2501_9462309812 小时前
Cordova&OpenHarmony提醒管理系统实现
android·javascript
shaominjin12314 小时前
Jetpack Compose 实践入门:从环境搭建到待办列表实现
android