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

        });
    }
};
相关推荐
用户20187928316719 分钟前
如何利用AI工具快速学习Android源码
android
音视频牛哥1 小时前
Android 平台RTSP/RTMP播放器SDK接入说明
android·音视频·大牛直播sdk·rtsp播放器·rtmp播放器·rtmp低延迟播放·rtmpplayer
aningxiaoxixi3 小时前
Android Framework 之 AudioDeviceBroker
android·windows·ffmpeg
~Yogi3 小时前
今日学习:工程问题(场景题)
android·学习
奔跑吧 android3 小时前
【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】
android·bluetooth·bt·aosp13
移动开发者1号3 小时前
Android Activity状态保存方法
android·kotlin
移动开发者1号3 小时前
Volley源码深度分析与设计亮点
android·kotlin
张风捷特烈3 小时前
每日一题 Flutter#7,8 | 关于 State 两道简答题
android·flutter·面试
计蒙不吃鱼11 小时前
一篇文章实现Android图片拼接并保存至相册
android·java·前端
LucianaiB12 小时前
如何做好一份优秀的技术文档:专业指南与最佳实践
android·java·数据库