Unity接入GME游戏多媒体引擎

离线语音课参考文章

https://blog.csdn.net/cyf649669121/article/details/85246017

官网文档

https://cloud.tencent.com/document/product/607/15228

官方SDK和demo下载网址

https://cloud.tencent.com/document/product/607/18521

错误码参照

https://cloud.tencent.com/document/product/607/15173

如果麦克风底噪声音很大,麦克风音量设置为25左右

int micVol = (int)(value * 100);
ITMGContext.GetInstance().GetAudioCtrl().SetMicVolume (micVol);

实时语音的demo

 		public static bool IsInitSucess { get; private set; }
 		// 主要测试方法
 		void testGme()、
 		{
 				int appID = 1400123456;
	        string appKey = "aacacaccacacccaca";
	        string CurChatRoomID = "romzzz_123";
	        string openID = UnityEngine.Random.Range(12345, 22345).ToString();
	        try
	        {
	           
	            QAVThreadHelper.CreateThreadHelper();
	            int ret = ITMGContext.GetInstance().Init(appID.ToString(), openID);
	            if (ret != QAVError.OK)
	            {
	                IsInitSucess = false;
	                Debug.LogError("语音初始化错误:" + ret);
	                return;
	            }
	            IsInitSucess = true;
	            var authBuffer = QAVAuthBuffer.GenAuthBuffer(appID, null, openID, appKey);
	            ITMGContext.GetInstance().GetPttCtrl().ApplyPTTAuthbuffer(authBuffer);
	            //初始化完成;
	            Debug.Log("语音聊天初始化完成!");
	        }
	        catch (Exception ex)
	        {
	            Debug.LogError("初始化语音聊天失败!\n" + ex.Message);
	            IsInitSucess = false;
	        }
	        //所有的回调,返回 ret == 0 表示成功,其余是报错;
	        //需要比对错误码表进行查证排错;
	
	        //进入聊天房间的回调:
	        ITMGContext.GetInstance().OnEnterRoomCompleteEvent += (int ret, string errInfo) => { };
	
	        //退出聊天房间;
	        ITMGContext.GetInstance().OnExitRoomCompleteEvent += () => { };
	
	        //房间成员变化;
	        //eventID 对应 ITMGContext 中的几个静态变量
	        //EVENT_ID_ENDPOINT_ENTER:成员进入房间
	        //EVENT_ID_ENDPOINT_EXIT:成员离开房间
	        //EVENT_ID_ENDPOINT_HAS_AUDIO:成员正在说话
	        //EVENT_ID_ENDPOINT_NO_AUDIO:成员停止说话
	        ITMGContext.GetInstance().OnEndpointsUpdateInfoEvent += (int eventID, int count, string[] ArrOpenID) => {
	            Debug.Log("OnEndpointsUpdateInfoEventcount=" + count+ " | eventID="+ eventID);
	            //进行处理
	            //开发者对参数进行解析,得到信息 event_id及 user_list
	            switch (eventID)
	            {
	                case 1:
	                    //有成员进入房间
	                    //开启麦克风;(上行)
	                    int ret = ITMGContext.GetInstance().GetAudioCtrl().EnableMic(true);
	
	                    //开启听筒;(下行)
	                    ret = ITMGContext.GetInstance().GetAudioCtrl().EnableSpeaker(true);
	                    break;
	                case 2:
	                    //有成员退出房间
	                    break;
	                case 5:
	                    //有成员发送音频包
	                    break;
	                case 6:
	                    //有成员停止发送音频包
	                    break;
	
	                default:
	                    break;
	            }
	        };
	        ITMGContext.GetInstance().EnterRoom(CurChatRoomID, ITMGRoomType.ITMG_ROOM_TYPE_FLUENCY, QAVAuthBuffer.GenAuthBuffer(appID, CurChatRoomID, openID, appKey));
 		}

QAVThreadHelper 类

public class QAVThreadHelper : MonoBehaviour
{
    public void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }

    public static QAVThreadHelper CreateThreadHelper()
    {
        GameObject obj = new GameObject("QAVThreadHelper");
        obj.hideFlags = HideFlags.HideAndDontSave;

        DontDestroyOnLoad(obj);
        QAVThreadHelper instance = obj.AddComponent<QAVThreadHelper>();

        UnityEngine.Debug.LogFormat("CreateThreadHelper:{0},{1}", obj.GetInstanceID(), instance);
        return instance;
    }

    public static bool DestroyThreadHelper(QAVThreadHelper helper)
    {
        if (helper)
        {
            if (helper.gameObject)
            {
                Destroy(helper.gameObject);
            }
            Destroy(helper);

            UnityEngine.Debug.LogFormat("DestroyThreadHelper:{0},{1}", helper.gameObject, helper);
        }

        return true;
    }

    public virtual void Update()
    {
        QAVNative.QAVSDK_Poll();
    }

    void OnApplicationFocus(bool hasFocus)
    {
        Debug.Log(string.Format("OnApplicationFocus {0}", hasFocus));
        if (hasFocus)
        {
            ITMGContext.GetInstance().Resume();
        }
        else
        {
            ITMGContext.GetInstance().Pause();
        }
    }

    void OnApplicationPause(bool pauseStatus)
    {
        Debug.Log(string.Format("OnApplicationPause {0}", pauseStatus));
    }
}
发布了69 篇原创文章 · 获赞 10 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/mhtqq809201/article/details/88636344