using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using YunvaIM; namespace YunvaIM { public class YunVaImSDK : MonoSingleton { #region 事件监听 public override void Init() { DontDestroyOnLoad(this); #region IM_LOGIN EventListenerManager.AddListener(ProtocolEnum.IM_THIRD_LOGIN_RESP, CPLoginResponse); #endregion #region IM_TOOL EventListenerManager.AddListener(ProtocolEnum.IM_RECORD_STOP_RESP, RecordStopRespInfo); EventListenerManager.AddListener(ProtocolEnum.IM_RECORD_FINISHPLAY_RESP, RecordFinishPlayRespInfo); EventListenerManager.AddListener(ProtocolEnum.IM_SPEECH_STOP_RESP, RecordRecognizeRespInfo); EventListenerManager.AddListener(ProtocolEnum.IM_UPLOAD_FILE_RESP, UploadFileRespInfo); EventListenerManager.AddListener(ProtocolEnum.IM_DOWNLOAD_FILE_RESP, DownLoadFileRespInfo); #endregion } #endregion #region 初始化SDK /// /// 初始化 /// /// 返回 0表示成功,-1表示失败 . /// 回调上下文. /// Appid. /// 保存数据库文件,提供路径. /// If set to 是否为测试环境,true为测试环境 is test. public int YunVa_Init(uint context, uint appid, string path, bool isTest, bool oversea=false) { YunvaLogPrint.YvDebugLog("YunVa_Init", string.Format("context:{0},appid:{1},path:{2},isTest:{3},oversea:{4}", context, appid, path, isTest, oversea)); return YunVaImInterface.instance.InitSDK(context, appid, path, isTest, oversea); } #endregion #region 登录 public System.Action ActionReLoginSuccess; private System.Action ActionLoginResponse; /// /// 云娃登录(第三方登录方式)CP接入推荐用这种方式 /// /// /// 服务器ID /// /// /// public void YunVaOnLogin(string tt, string gameServerID, string[] wildCard, int readStatus, System.Action Response) { YunvaLogPrint.YvDebugLog ("YunVaOnLogin", string.Format ("tt:{0},gameServerID:{1},readStatus:{2},1", tt, gameServerID, readStatus)); ActionLoginResponse = Response; uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_string(parser, 1, tt); YunVaImInterface.instance.YVparser_set_string(parser, 2, gameServerID); for (int i = 0; i < wildCard.Length;i++ ) { YunvaLogPrint.YvDebugLog("YunVaOnLogin",string.Format("wildCard-{0}:{1}",i,wildCard[i])); YunVaImInterface.instance.YVparser_set_string(parser, 3, wildCard[i]); } YunVaImInterface.instance.YVparser_set_uint8(parser, 4, readStatus); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_LOGIN, (uint)ProtocolEnum.IM_THIRD_LOGIN_REQ, parser); } /// /// 登出帐号 /// public void YunVaLogOut() { YunvaLogPrint.YvDebugLog ("YunVaLogOut", "YunVaLogOut..."); uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_LOGIN, (uint)ProtocolEnum.IM_LOGOUT_REQ, parser); } private void CPLoginResponse(object data) { if (data is ImThirdLoginResp) { ImThirdLoginResp dataResp = new ImThirdLoginResp(); if ( ActionLoginResponse != null) { ActionLoginResponse((ImThirdLoginResp)data); ActionLoginResponse = null; } } } private void ReLoginNotify(object data) { if( ActionReLoginSuccess!=null) { ActionReLoginSuccess(); } } #endregion #region 工具 public System.Action RecordingCallBack;//录音回调 /// /// 开始录音(最长60秒) /// /// /// public int RecordStartRequest(string filePath, int speech=0, string ext = "") { YunvaLogPrint.YvDebugLog("RecordStartRequest", string.Format("filePath:{0},speech:{1},ext:{2}", filePath, speech, ext)); if(RecordingCallBack!=null) RecordingCallBack(true); uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_string(parser, 1, filePath); YunVaImInterface.instance.YVparser_set_string(parser, 2, ext); YunVaImInterface.instance.YVparser_set_integer(parser, 3, speech); int ret = YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_RECORD_STRART_REQ, parser); Debug.Log("RecordStartRequest ret=" + ret); return ret; } private System.Action ActionRecordStopResponse; /// /// 停止录音请求 回调返回录音文件路径名 /// /// 返回的回调 public void RecordStopRequest(System.Action StopResponse, System.Action UploadResp=null,System.Action SpeechResp=null) { YunvaLogPrint.YvDebugLog ("RecordStopRequest", "RecordStopRequest..."); if(RecordingCallBack!=null) RecordingCallBack(false); ActionRecordStopResponse = StopResponse; ActionUploadFileResp = UploadResp; ActionRecognizeResp = SpeechResp; uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_RECORD_STOP_REQ, parser); } private void RecordStopRespInfo(object data) { if (data is ImRecordStopResp) { if( ActionRecordStopResponse!=null) { ActionRecordStopResponse((ImRecordStopResp)data); //ActionRecordStopResponse = null; } } } private Dictionary> RecordFinishPlayRespMapping = new Dictionary>(); /// /// 播放录音请求 /// /// 录音的url路径 /// 回调方法 /// 录音文件路径 (可以不必两者都传 但至少要传入一个) /// 扩展标记 public int RecordStartPlayRequest(string filePath, string url, string ext, System.Action Response) { YunvaLogPrint.YvDebugLog ("RecordStartPlayRequest", string.Format ("filePath:{0},url:{1},ext:{2}", filePath, url,ext)); if(!RecordFinishPlayRespMapping.ContainsKey(ext)){ RecordFinishPlayRespMapping.Add(ext, Response); } uint parser = YunVaImInterface.instance.YVpacket_get_parser(); if(!string.IsNullOrEmpty(url)){ YunVaImInterface.instance.YVparser_set_string(parser, 1, url); } if(!string.IsNullOrEmpty(filePath)){ YunVaImInterface.instance.YVparser_set_string(parser, 2, filePath); } else{ Debug.Log(string.Format("{0}: is url voice", url)); YunVaImInterface.instance.YVparser_set_string(parser, 2, ""); } YunVaImInterface.instance.YVparser_set_string(parser, 3, ext); return YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_RECORD_STARTPLAY_REQ, parser); } private void RecordFinishPlayRespInfo(object data) { if (data is ImRecordFinishPlayResp) { ImRecordFinishPlayResp reData = (ImRecordFinishPlayResp)data; string key = reData.ext; System.Action callback; if(RecordFinishPlayRespMapping.TryGetValue(key, out callback)){ if(callback != null){ callback(reData); } RecordFinishPlayRespMapping.Remove(key); } else{ Debug.Log(key + ": callback not found"); } } } /// /// 停止播放语音 /// public void RecordStopPlayRequest() { YunvaLogPrint.YvDebugLog ("RecordStopPlayRequest","RecordStopPlayRequest..."); uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_RECORD_STOPPLAY_REQ, parser); } //private Dictionary> RecognizeRespMapping = new Dictionary>(); System.Action ActionRecognizeResp; /// /// 开始语音识别 /// /// /// /// public void SpeechStartRequest(string filePath, string ext, System.Action Response,int type=(int)yvspeech.speech_file,string url="") { YunvaLogPrint.YvDebugLog ("SpeechStartRequest", string.Format ("filePath:{0},ext:{1},type:{2},url:{3}", filePath, ext,type,url)); //string ext = DateTime.Now.ToFileTime().ToString(); //RecognizeRespMapping.Add(ext, Response); ActionRecognizeResp = Response; uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_string(parser, 1, filePath); YunVaImInterface.instance.YVparser_set_string(parser, 2, ext); YunVaImInterface.instance.YVparser_set_integer(parser, 3, type); YunVaImInterface.instance.YVparser_set_string(parser, 4, url); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_SPEECH_START_REQ, parser); } private void RecordRecognizeRespInfo(object data) { if (data is ImSpeechStopResp) { if (ActionRecognizeResp != null) { ActionRecognizeResp((ImSpeechStopResp)data); //ActionRecognizeResp = null; } /* Debug.Log("record recognize..."); ImSpeechStopResp reData = (ImSpeechStopResp)data; string key = reData.ext; System.Action callback; if(RecognizeRespMapping.TryGetValue(key, out callback)){ if(callback != null){ callback(reData); } RecognizeRespMapping.Remove(key); } * */ } } /// /// 设置语音识别语言 /// /// public void SpeechSetLanguage(Yvimspeech_language langueage=Yvimspeech_language.im_speech_zn,yvimspeech_outlanguage outlanguage=yvimspeech_outlanguage.im_speechout_simplified) { YunvaLogPrint.YvDebugLog ("SpeechSetLanguage", string.Format ("langueage:{0},outlanguage:{1}", langueage, outlanguage)); uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_integer(parser, 1, (int)langueage); YunVaImInterface.instance.YVparser_set_integer(parser, 2, (int)outlanguage); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_SPEECH_SETLANGUAGE_REQ, parser); } private System.Action ActionUploadFileResp; /// /// 上传文件 /// /// /// /// public void UploadFileRequest(string filePath,string fileId, System.Action Response) { YunvaLogPrint.YvDebugLog ("UploadFileRequest", string.Format ("filePath:{0},fileId:{1}", filePath, fileId)); ActionUploadFileResp = Response; uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_string(parser, 1, filePath); YunVaImInterface.instance.YVparser_set_string(parser, 2, fileId); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_UPLOAD_FILE_REQ, parser); } private void UploadFileRespInfo(object data) { if (data is ImUploadFileResp) { if (ActionUploadFileResp != null) { ActionUploadFileResp((ImUploadFileResp)data); //ActionUploadFileResp = null; } } } private System.Action ActionDownLoadFileResp; /// /// 下载文件请求 /// /// /// /// /// public void DownLoadFileRequest(string url, string filePath, string fileid, System.Action Response) { YunvaLogPrint.YvDebugLog ("DownLoadFileRequest", string.Format ("url:{0},filePath:{1},fileid:{2}",url,filePath, fileid)); ActionDownLoadFileResp = Response; uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_string(parser, 1, url); YunVaImInterface.instance.YVparser_set_string(parser, 2, filePath); YunVaImInterface.instance.YVparser_set_string(parser, 3, fileid); YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_DOWNLOAD_FILE_REQ, parser); } private void DownLoadFileRespInfo(object data) { if (data is ImDownLoadFileResp) { if( ActionDownLoadFileResp!=null) { ActionDownLoadFileResp((ImDownLoadFileResp)data); ActionDownLoadFileResp = null; } } } // /// /// 设置音量信息。 /// /// 声音长度 /// true为返回音量,false不返回音量 public void RecordSetInfoReq(bool isVolume=false) { YunvaLogPrint.YvDebugLog ("RecordSetInfoReq", string.Format ("isVolume:{0}",isVolume)); RecordSetInfoReq(isVolume,60); } public void RecordSetInfoReq(bool isVolume,int length) { YunvaLogPrint.YvDebugLog ("RecordSetInfoReq", string.Format ("isVolume:{0},length:{1}",isVolume,length)); uint parser = YunVaImInterface.instance.YVpacket_get_parser(); if(isVolume) { YunVaImInterface.instance.YVparser_set_integer(parser, 1, length); YunVaImInterface.instance.YVparser_set_integer(parser, 2, 1); } else { YunVaImInterface.instance.YVparser_set_integer(parser, 1, length); YunVaImInterface.instance.YVparser_set_integer(parser, 2, 0); } YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_RECORD_SETINFO_REQ, parser); } public bool CheckCacheFile(string url){ YunvaLogPrint.YvDebugLog ("CheckCacheFile", string.Format ("url:{0}",url)); uint parser = YunVaImInterface.instance.YVpacket_get_parser(); YunVaImInterface.instance.YVparser_set_string(parser, 1, url); int ret = YunVaImInterface.instance.YV_SendCmd(CmdChannel.IM_TOOLS, (uint)ProtocolEnum.IM_TOOL_HAS_CACHE_FILE, parser); return ret == 0; } #endregion /// /// 是否打印日志 /// /// LOG_LEVEL_OFF = 0, //0:关闭日志,LOG_LEVEL_DEBUG = 1 //1:Debug默认该级别 public void YvLog_setLogLevel(int logLevel = (int)YvlogLevel.LOG_LEVEL_DEBUG) { YunvaLogPrint.YvLog_setLogLevel(logLevel); } } }