文章目录

  • unity3调用Android 封装的声网SDK
  • 1、环境版本
  • 2、创建Android library 工程
  • 3、unity3D 依赖包添加到工程libs下
  • 4、UnityPlayerActivity 添加到项目中
  • 5、自定义Activity 继承 UnityPlayerActivity
  • 6、清单文件设置
  • 7、build.gradle 添加 jar 脚本
  • 8、集成 声网sdk
    • 8.1 so库 添加到项目中
    • 8.2 声网jar 添加 libs 目录下
    • 8.4 编写 集成声网sdk的工具类
    • 8.5 MainActivity中 提供方法给 unity 调用
  • 9、jar 生成
  • 10、 unity中集成‘
    • 10.1Assets 文件下 新建 Plugins 目录
    • 10.2 AndroidPlugin.jar 放在 bin 目录下
    • 10.3 声网so 库和jar 放在 libs目录下
    • 10.4 清单文件放在Android目录下
  • 11、 unity 脚本中调用 android 提供的方法
  • 12、参考 资料

unity3调用Android 封装的声网SDK

1、环境版本

unity3D 编辑器 2020

Android Studio 4.2

2、创建Android library 工程

3、unity3D 依赖包添加到工程libs下

依赖包的位置(unity 安装目录下):E:\Unity_install\2020.3.28f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes

4、UnityPlayerActivity 添加到项目中

添加到项目中

5、自定义Activity 继承 UnityPlayerActivity

注销掉 设置布局方法

6、清单文件设置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.xyx.astounity"><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><application><activity android:name="com.xyx.astounity.MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><meta-data android:name="unityplayer.UnityActivity" android:value="true" /></activity></application></manifest>

7、build.gradle 添加 jar 脚本

//----------------这是一组将module导出为jar的gradle命令-------------------
task deleteOldJar(type: Delete) {delete 'release/AndroidPlugin.jar'
}//task to export contents as jar
task exportJar(type: Copy) {from('build/intermediates/aar_main_jar/release/')into('release/')include('classes.jar')///Rename the jarrename('classes.jar', 'AndroidPlugin.jar')
}exportJar.dependsOn(deleteOldJar, build)

8、集成 声网sdk

8.1 so库 添加到项目中

8.2 声网jar 添加 libs 目录下

8.4 编写 集成声网sdk的工具类

package com.xyx.astounity;import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;import io.agora.rtc.Constants;
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;public class VoiceUtils {private String appKey="5c203ec6100d4f1bbbc79265a81ef0f0";private String token="0065c203ec6100d4f1bbbc79265a81ef0f0IABoJ4pt255quepmrqcN+P+MzHNWvu489IlD6C43eOajEKDfQtYAAAAAEAAn2yw6p4gZYgEAAQC/iBli";private String channel="demo";private RtcEngine mRtcEngine;private Handler handler=new Handler(Looper.getMainLooper());private Context context;public boolean isIsSpeak(){return isSpeak;}public void initializeAgoraEngine(Context context){try {mRtcEngine = RtcEngine.create(context, appKey, mRtcEventHandler);this.context=context;int i = mRtcEngine.enableDeepLearningDenoise(true);mRtcEngine.enableAudioVolumeIndication(100,3,true);Log.e("zyb", "initializeAgoraEngine: "+i );} catch (Exception e) {throw new RuntimeException("NEED TO check rtc sdk init fatal error\n" + Log.getStackTraceString(e));}}public void joinChannel() {String accessToken = token;if (TextUtils.equals(accessToken, "") || TextUtils.equals(accessToken, "#YOUR ACCESS TOKEN#")) {accessToken = null; // default, no token}// Sets the channel profile of the Agora RtcEngine.// CHANNEL_PROFILE_COMMUNICATION(0): (Default) The Communication profile. Use this profile in one-on-one calls or group calls, where all users can talk freely.// CHANNEL_PROFILE_LIVE_BROADCASTING(1): The Live-Broadcast profile. Users in a live-broadcast channel have a role as either broadcaster or audience. A broadcaster can both send and receive streams; an audience can only receive streams.mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION);// Allows a user to join a channel.mRtcEngine.joinChannel(accessToken, channel, "Extra Optional Data", 0); // if you do not specify the uid, we will generate the uid for youmRtcEngine.enableAudioVolumeIndication(50,3,true);}private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {// Tutorial Step 1/*** Occurs when a remote user (Communication)/host (Live Broadcast) leaves the channel.** There are two reasons for users to become offline:**     Leave the channel: When the user/host leaves the channel, the user/host sends a goodbye message. When this message is received, the SDK determines that the user/host leaves the channel.*     Drop offline: When no data packet of the user or host is received for a certain period of time (20 seconds for the communication profile, and more for the live broadcast profile), the SDK assumes that the user/host drops offline. A poor network connection may lead to false detections, so we recommend using the Agora RTM SDK for reliable offline detection.** @param uid ID of the user or host who* leaves* the channel or goes offline.* @param reason Reason why the user goes offline:**     USER_OFFLINE_QUIT(0): The user left the current channel.*     USER_OFFLINE_DROPPED(1): The SDK timed out and the user dropped offline because no data packet was received within a certain period of time. If a user quits the call and the message is not passed to the SDK (due to an unreliable channel), the SDK assumes the user dropped offline.*     USER_OFFLINE_BECOME_AUDIENCE(2): (Live broadcast only.) The client role switched from the host to the audience.*/@Overridepublic void onUserOffline(final int uid, final int reason) { // Tutorial Step 4//            runOnUiThread(new Runnable() {//                @Override
//                public void run() {//                    onRemoteUserLeft(uid, reason);
//                }
//            });}@Overridepublic void onAudioVolumeIndication(AudioVolumeInfo[] speakers, int totalVolume) {super.onAudioVolumeIndication(speakers, totalVolume);
//            Log.e("zyb", "onAudioVolumeIndication: "+uid );if (speakers.length == 1) {int uid = speakers[0].uid;int vad = speakers[0].vad;Log.e("zyb", "onAudioVolumeIndication: "+uid );if (uid != 0 ) {//if (vad == 1) {try {isSpeak = true;} catch (Exception e) {e.printStackTrace();}} else {isSpeak = false;}}}else {isSpeak=false;}}/*** Occurs when a remote user stops/resumes sending the audio stream.* The SDK triggers this callback when the remote user stops or resumes sending the audio stream by calling the muteLocalAudioStream method.** @param uid ID of the remote user.* @param muted Whether the remote user's audio stream is muted/unmuted:**     true: Muted.*     false: Unmuted.*/@Overridepublic void onUserMuteAudio(final int uid, final boolean muted) { // Tutorial Step 6handler.post(() -> Toast.makeText(context,"用户:"+uid +" \t 加入房间 ",Toast.LENGTH_SHORT).show());}};public static volatile boolean isSpeak = false;
}

8.5 MainActivity中 提供方法给 unity 调用

 public boolean  isIsSpeak(){if (voiceUtils==null){Toast.makeText(this,"对象没有初始化",Toast.LENGTH_SHORT).show();return false;}return voiceUtils.isIsSpeak();}public void initializeAgoraEngine(){voiceUtils.initializeAgoraEngine(this);}public void joinChannel(){if (voiceUtils==null){Toast.makeText(this,"对象没有初始化",Toast.LENGTH_SHORT).show();return;}voiceUtils.joinChannel();}

9、jar 生成

生成 目录

10、 unity中集成‘

10.1Assets 文件下 新建 Plugins 目录

10.2 AndroidPlugin.jar 放在 bin 目录下

10.3 声网so 库和jar 放在 libs目录下

10.4 清单文件放在Android目录下

11、 unity 脚本中调用 android 提供的方法

unity的包名和Android的版本设置的和jar包的一样

初始化 声网sdk的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if(UNITY_2018_3_OR_NEWER)
using UnityEngine.Android;
#endifpublic class CallAndroid : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){Permission.RequestUserPermission("android.permission.RECORD_AUDIO");#if (UNITY_2018_3_OR_NEWER)if (Permission.HasUserAuthorizedPermission(Permission.Microphone)){}else{Permission.RequestUserPermission(Permission.Microphone);}
#endif}// Update is called once per framevoid Update(){}private AndroidJavaClass jc;private AndroidJavaObject jo;private void OnGUI(){if (GUILayout.Button("Call Android",GUILayout.Width(200), GUILayout.Height(100))){//这两行是固定写法jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");jo = jc.GetStatic<AndroidJavaObject>("currentActivity");int result =jo.Call<int>("add",1,2);jo.Call("UnityCallAndroid");// bool isSpeek = jo.Call<bool>("isIsSpeak");}if (GUILayout.Button("init", GUILayout.Width(200), GUILayout.Height(100))){//这两行是固定写法jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");jo = jc.GetStatic<AndroidJavaObject>("currentActivity");jo.Call("initializeAgoraEngine");}if (GUILayout.Button("join", GUILayout.Width(200), GUILayout.Height(100))){//这两行是固定写法jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");jo = jc.GetStatic<AndroidJavaObject>("currentActivity");jo.Call("joinChannel");}}public void UnityMethod(string str){Debug.Log("UnityMethod被调用,参数:" + str);}
}

判断声网sdk VAD 脚本

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;public class Switch : MonoBehaviour
{// Start is called before the first frame updateprivate AndroidJavaClass jc;private AndroidJavaObject jo;long currentTime=0;GameObject a;GameObject b;void Start(){a = GameObject.Find("a_demo");b = GameObject.Find("b");}bool flag=false;// Update is called once per frame[System.Obsolete]void Update(){//这两行是固定写法jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");jo = jc.GetStatic<AndroidJavaObject>("currentActivity");bool isSpeek = jo.Call<bool>("isIsSpeak");Debug.Log("isSpeek::::====" + isSpeek);Thread.Sleep(300);if (isSpeek){//Thread.Sleep(200);a.SetActive(flag);b.SetActive(!flag);flag = !flag;Debug.Log("-----" + flag);// a.SetActive(true);// b.SetActive(false);}else {if (b.active){b.SetActive(false);a.SetActive(true);flag = !flag;}}}
}

12、参考 资料

https://www.cnblogs.com/Jason-c/p/6743224.html

https://blog.csdn.net/u014361280/article/details/107692986

https://www.kancloud.cn/hujunjob/unity/139595

Unity调用Android封装的声网sdk相关推荐

  1. Unity Metaverse(八)、RTC Engine 基于Agora声网SDK实现音视频通话

    文章目录 简介 创建应用 构建应用场景 API调用与回调事件 测试 简介 本文介绍如何在Unity中接入声网SDK,它可以应用的场景有许多,例如直播.电商.游戏.社交等,音视频通话是其实时互动的基础能 ...

  2. 用声网 Android UIKit 为实时视频通话应用添加自定义背景丨声网 SDK 教程

    使用声网 SDK 和 UIKit 创建视频推流应用非常简单,而且声网还有许多功能,可以提高视频通话的质量和便利性.例如,我们可以在视频通话过程中使用虚拟背景,为视频通话增添趣味性. 我们可以通过以下三 ...

  3. 基于 Web SDK 实现视频通话场景 | 声网 SDK 教程

    声网视频 SDK 被广泛应用于多种实时互动场景中,例如视频会议.视频通话.音视频社交.在线教育等.为了让刚刚接触声网 SDK 的开发者,可以更顺畅地实现基础的视频通话功能,我们基于声网 Web SDK ...

  4. 基于声网 SDK 实现 iOS 端的一对一视频通话

    在很多产品,加入实时视频通话已经不是新鲜事情了,尤其是近几年的疫情影响,个人公司国家对于实时音视频的需求一直在快速的增长.例如视频会议.社交应用.实时游戏,甚至也可能出现在一些元宇宙的场景中. 本文将 ...

  5. 小程序直播声网SDK使用

    1.注册声网账号 2.添加小程序开发所需服务端域名 3.下载SDK引入 初始化 4.引入项目 // <--------声网SDK接入---------> var AgoraMiniappS ...

  6. 使用声网sdk音视频时,播放本地音频心得

    注意: 使用声网sdk音视频时,需要使用声网sdk内部的播放音频api 否则在iOS12系统上,通过avplayer播放时,使用replaceItem的方式切换AVPlayerItem时会出现声音消失 ...

  7. Unity调用android相册获取图片或视频

    Unity调用android相册获取图片或视频 (此文章对有unity基础和对环境配置有基础的童嚡容易看懂) 因为项目上用到,在百度了很多大佬的文章后,陆陆续续踩了很多坑,可能是我哪里设置的不对,大部 ...

  8. android视频分享功能吗,Unity 调用 Android 分享功能(基于ShareRec SDK视频分享)

    需求 Mob 平台是一个强大的提供分享功能的平台,为移动开发者提供 ShareSDK 社交分享.ShareREC 手游录像分享.短信验证码 SDK 及 BigApp 等免费服务. Unity 使用 S ...

  9. Android大疆无人机对接声网sdk

    项目下gradle导入 implementation 'io.agora.rtc:agora-full-beta:4.0.0-beta.1' 版本根据自身修改 public class AgoraUt ...

最新文章

  1. Hadoop.2.x_HA部署
  2. monty python life of brian-50大最搞笑喜剧片,无厘头成必杀技!
  3. MySQL第4天:MySQL的架构介绍之修改数据库编码格式
  4. 计算机视觉实时目标检测 TensorFlow Object Detection API
  5. 小白开学Asp.Net Core 《九》
  6. python代码太长_Python 太糟糕了?开发者总结了 8 大原因
  7. C语言排序方法-----希尔排序
  8. VB.NET工作笔记004---认识wsf文件
  9. 《云周刊》69期:开门红利!阿里云2月活动来袭
  10. python数据保存为excel_Python读excel生成数据存入txt文件
  11. 宗成庆《自然语言理解》第三章作业
  12. vivado 开发教程(一) 创建新硬件工程
  13. [licode cs交互] 1 android client 连接到token服务器
  14. VMware虚拟机复制文件卡死的问题
  15. 我是如何从通信转到Java软件开发工程师的?
  16. Method threw ‘java.lang.IllegalStateException‘ exception. Cannot evaluate org.apache.hadoop.mapreduc
  17. 对接支付通道如何收费?支付接口收费标准
  18. 浪潮ps9.0 oracle,浪潮ERP-PS V9.1安装手册.doc
  19. Gradle基础:4:Task的使用方式
  20. matlab+cummean,matlab常用操作备忘(2)

热门文章

  1. 算法训练营 图的应用(最小生成树)
  2. Linux桌面录屏分享
  3. (Java)socket网络编程及处理socket粘包拆包问题
  4. 联想xt92耳机测评
  5. Java国际化:BreakIterator
  6. 共享文件夹Windows和Linux双向粘贴
  7. [原创]作弊教室-你想作弊?小心旁边的人成绩比你还差
  8. Unity 快速检测本机网络链接状态
  9. Oracle Chp3 复杂查询 key point:数据分页;子查询;表连接;集合运算符
  10. ACM在线评测系统 各大高校的ACM在线测评系统