using System; using System.Collections; using System.Collections.Generic; using Games.ChatHistory; using UnityEngine; #if UNITY_EDITOR #else using YunvaIM; #endif public class ChatVoice : UIControllerBase { public delegate void ChatVoiceStopPlayCallBack(string url); public delegate void ChatVoiceUpLoadCallBack(string url, string text, float voiceTime, string errorMsg, Hashtable hashParam); //1002143 刀剑问情 // ReSharper disable once InconsistentNaming public static uint _APPID = 1002143; // ReSharper disable once FieldCanBeMadeReadOnly.Local private static bool _IsVoiceInit = false; public int _VoiceVolume = 50; public float _VoiceTime; public string _TempPath = ""; public string _TempUrl = ""; public float _RecordTime; private string _PlayingUrl = ""; private float _ResumeTime; private ChatVoiceStopPlayCallBack _StopPlayCallBack; public void ChatVoiceInit() { if (_IsVoiceInit) return; #if !UNITY_EDITOR try { EventListenerManager.AddListener(ProtocolEnum.IM_RECORD_VOLUME_NOTIFY, ImRecordVolume);//录音音量大小回调监听 int init = YunVaImSDK.instance.YunVa_Init(0, _APPID, Application.persistentDataPath, false, false); if (init == 0) { ChatVoiceLogin(); } else _IsVoiceInit = false; } catch (Exception e) { Debug.Log("Voice error:" + e); } #endif } public void ChatVoiceLogin() { var guid = LoginData._LoadingRoleGuid; //var guid = 123; var ttFormat = "{{\"nickname\":\"{0}\",\"uid\":\"{1}\"}}"; var tt = string.Format(ttFormat, guid, guid); var wildcard = new string[2]; wildcard[0] = "0x001"; wildcard[1] = "0x002"; try { #if UNITY_EDITOR #else YunVaImSDK.instance.YunVaOnLogin(tt, "100", wildcard, 0, (data) => { if (data.result == 0) { _IsVoiceInit = true; YunVaImSDK.instance.RecordSetInfoReq(true);//开启录音的音量大小回调 } else { GUIData.AddNotifyData(data.msg); _IsVoiceInit = false; } }); YunVaImSDK.instance.SpeechSetLanguage(Yvimspeech_language.im_speech_zn,yvimspeech_outlanguage.im_speechout_simplified); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } public void ChatVoiceStartRecord() { if (!_IsVoiceInit) { ChatVoiceInit(); return; } var filePath = string.Format("{0}/{1}.amr", Application.persistentDataPath, DateTime.Now.ToFileTime()); SoundManager.SetGlobalVolumn(0); try { #if !UNITY_EDITOR YunVaImSDK.instance.RecordStartRequest(filePath, 1); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } public void ChatVoiceFinishRecord(ChatVoiceUpLoadCallBack callBack, Hashtable hash) { float time = 0; var recordUrlPath = ""; var text = ""; SoundManager.SetGlobalVolumn(PlayerPreferenceData.SystemSoundVolum); var errorMsg = ""; try { #if UNITY_EDITOR time = 3; text = "123"; callBack.Invoke(recordUrlPath, text, time, errorMsg, hash); #else YunVaImSDK.instance.RecordStopRequest((data1) => { _RecordTime = data1.time * 0.001f; _TempPath = data1.strfilepath; if(!string.IsNullOrEmpty(data1.msg)) { if(string.IsNullOrEmpty(errorMsg)) { errorMsg = data1.msg; } } }, (data2) => { recordUrlPath = data2.fileurl; _TempUrl = recordUrlPath; if(!string.IsNullOrEmpty(data2.msg)) { if(string.IsNullOrEmpty(errorMsg)) { errorMsg = data2.msg; } } }, (data3) => { if(!string.IsNullOrEmpty(data3.msg)) { if(string.IsNullOrEmpty(errorMsg)) { errorMsg = data3.msg; } } text = data3.text; if (callBack != null) { callBack.Invoke(recordUrlPath, text, _RecordTime, errorMsg, hash); } }); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } public void ChatVoiceStopRecord() { try { #if !UNITY_EDITOR YunVaImSDK.instance.RecordStopRequest(null, null, null); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } public void ChatVoicePlay(string url, float playTime, ChatHistoryItem chatItem, ChatVoiceStopPlayCallBack stopPlayCallBack = null) { var ext = DateTime.Now.ToFileTime().ToString(); ChatVoiceStopPlay(); _StopPlayCallBack = stopPlayCallBack; _PlayingUrl = url; SoundManager.SetGlobalVolumn(0); StopCoroutine("ResumeMusic"); _ResumeTime = playTime; StartCoroutine("ResumeMusic"); _PlayingUrl = url; _PlayingItem = chatItem; if (ChatInfoLogic.Instance() && ChatInfoLogic.Instance().gameObject.activeSelf) ChatInfoLogic.Instance().UpdateVoiceAnim(); if (FriendAndMailRoot.Instance() && FriendAndMailRoot.Instance()._FriendRootLogic.isActiveAndEnabled) FriendAndMailRoot.Instance()._FriendRootLogic._ChatHistoryLogic.UpdateVoiceAnim(); try { #if !UNITY_EDITOR var result = YunVaImSDK.instance.RecordStartPlayRequest("", url, ext, (data2) => { if (data2.result == 0) { GuiTextDebug.debug("ChatVoicePlay:Sucess"); } else { GuiTextDebug.debug("ChatVoicePlay:1" + data2.describe); } }); GuiTextDebug.debug("ChatVoicePlay result:" + result); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } public IEnumerator ResumeMusic() { yield return new WaitForSeconds(_ResumeTime); FinishPlay(); } public void ChatVoiceStopPlay() { FinishPlay(); try { #if UNITY_EDITOR #else YunVaImSDK.instance.RecordStopPlayRequest(); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } private void FinishPlay() { _PlayingItem = null; SoundManager.SetGlobalVolumn(PlayerPreferenceData.SystemSoundVolum); if (_StopPlayCallBack != null) _StopPlayCallBack.Invoke(_PlayingUrl); _PlayingUrl = ""; if (ChatInfoLogic.Instance() && ChatInfoLogic.Instance().gameObject.activeSelf) ChatInfoLogic.Instance().UpdateVoiceAnim(); if (FriendAndMailRoot.Instance() && FriendAndMailRoot.Instance()._FriendRootLogic.isActiveAndEnabled) FriendAndMailRoot.Instance()._FriendRootLogic._ChatHistoryLogic.UpdateVoiceAnim(); } public void ChatVoiceLogout() { try { #if UNITY_EDITOR #else YunVaImSDK.instance.YunVaLogOut(); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } public void ImRecordVolume(object data) { try { #if UNITY_EDITOR #else ImRecordVolumeNotify RecordVolumeNotify = data as ImRecordVolumeNotify; _VoiceVolume = RecordVolumeNotify.v_volume; Debug.Log("ImRecordVolumeNotify:v_volume=" + RecordVolumeNotify.v_volume); #endif } catch (Exception e) { Debug.Log("Voice error:" + e); } } #region private void Awake() { SetInstance(this); } private void OnDestory() { SetInstance(null); } #endregion #region auto voice public bool _IsAutoVoice = true; private readonly List _AutoPlayList = new List(); private ChatHistoryItem _PlayingItem; public ChatHistoryItem GetPlayingChat() { return _PlayingItem; } public void AutoVoice(ChatHistoryItem chatItem) { _AutoPlayList.Add(chatItem); PlayNext(""); } private void PlayAutoList() { if (string.IsNullOrEmpty(_PlayingUrl)) PlayNext(""); } private void PlayNext(string playingUrl) { if (_PlayingItem != null) return; if (_AutoPlayList.Count == 0) return; ChatHistoryItem chatItem = null; for (var i = 0; i < _AutoPlayList.Count; ++i) { var voiceIetm = _AutoPlayList[i]; if (voiceIetm.EChannel == GC_CHAT.CHATTYPE.CHAT_TYPE_WORLD && !PlayerPreferenceData.SystemAutoVoiceWorld) continue; if (voiceIetm.EChannel == GC_CHAT.CHATTYPE.CHAT_TYPE_GUILD && !PlayerPreferenceData.SystemAutoVoiceGuild) continue; if (voiceIetm.EChannel == GC_CHAT.CHATTYPE.CHAT_TYPE_TEAM && !PlayerPreferenceData.SystemAutoVoiceTeam) continue; if (voiceIetm.EChannel == GC_CHAT.CHATTYPE.CHAT_TYPE_NORMAL && !PlayerPreferenceData.SystemAutoVoiceNearBy) continue; chatItem = voiceIetm; break; } if (chatItem == null) return; var splitStrs = chatItem.ChatInfo.Split(ChatLinkBase.StrSplit); if (splitStrs.Length != 4) return; var url = splitStrs[1]; var second = float.Parse(splitStrs[2]); if (_StopPlayCallBack == PlayNext) _StopPlayCallBack = null; chatItem._ReadTip = true; ChatVoicePlay(url, second, chatItem, PlayNext); _AutoPlayList.Remove(chatItem); } #endregion }