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

        });
    }
};
相关推荐
曾经的三心草5 分钟前
Mysql之约束与事件
android·数据库·mysql·事件·约束
guoruijun_2012_44 小时前
fastadmin多个表crud连表操作步骤
android·java·开发语言
Winston Wood4 小时前
一文了解Android中的AudioFlinger
android·音频
B.-6 小时前
Flutter 应用在真机上调试的流程
android·flutter·ios·xcode·android-studio
有趣的杰克6 小时前
Flutter【04】高性能表单架构设计
android·flutter·dart
大耳猫11 小时前
主动测量View的宽高
android·ui
帅次14 小时前
Android CoordinatorLayout:打造高效交互界面的利器
android·gradle·android studio·rxjava·android jetpack·androidx·appcompat
枯骨成佛14 小时前
Android中Crash Debug技巧
android
kim565920 小时前
android studio 更改gradle版本方法(备忘)
android·ide·gradle·android studio
咸芝麻鱼20 小时前
Android Studio | 最新版本配置要求高,JDK运行环境不适配,导致无法启动App
android·ide·android studio