Files
JJBB/Assets/Project/Script/GUI/Chat/ChatVoiceInputLogic.cs
2024-08-23 15:49:34 +08:00

233 lines
6.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections;
using Games.ChatHistory;
using GCGame;
using GCGame.Table;
using System;
using Games.GlobeDefine;
using Module.Log;
public class ChatVoiceInputLogic : UIControllerBase<ChatVoiceInputLogic>
{
public static bool InputVoice(GC_CHAT.CHATTYPE chatCannel = GC_CHAT.CHATTYPE.CHAT_TYPE_INVALID)
{
_ChatCannel = chatCannel;
UIManager.ShowUI(UIInfo.ChatVoiceInputRoot);
return true;
//#if UNITY_ANDROID && UNITY_2018
// if (UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.Microphone))
// {
//#elif UNITY_ANDROID
// if (PlatformHelper.requstePremission("android.permission.RECORD_AUDIO"))
// {
//#endif
// UIManager.ShowUI(UIInfo.ChatVoiceInputRoot);
// return true;
//#if UNITY_ANDROID && UNITY_2018
// }
// else
// {
// UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.Microphone);
// return false;
// }
//#elif UNITY_ANDROID
// }
// else
// {
// return false;
// }
//#endif
}
public static void StopVoice()
{
if (!ChatVoiceInputLogic.Instance())
return;
ChatVoiceInputLogic.Instance().StopInputVoice();
}
public static void CancelVoiceSlider()
{
if (!ChatVoiceInputLogic.Instance())
return;
ChatVoiceInputLogic.Instance().CancelSlider();
}
public static void CancelVoiceKeyUp()
{
if (!ChatVoiceInputLogic.Instance())
return;
ChatVoiceInputLogic.Instance().CancelKeyUp();
}
public void OnEnable()
{
SetInstance(this);
StartInputVoice();
}
public void OnDisable()
{
SetInstance(null);
}
public void Update()
{
UpdateVolumn();
}
#region
public GameObject _VoicePanel;
public GameObject _CancelPanel;
public GameObject[] _VolumnGOs;
private ChatInputLogic _InputLogic;
private static GC_CHAT.CHATTYPE _ChatCannel = GC_CHAT.CHATTYPE.CHAT_TYPE_INVALID;
private bool _Inputing = false;
private static int _CharCnt = 72;
public void StartInputVoice()
{
if (ChatInputLogic.Instance() && ChatInputLogic.Instance().gameObject.activeSelf)
{
_InputLogic = ChatInputLogic.Instance();
}
else if(FriendAndMailRoot.Instance() && FriendAndMailRoot.Instance().gameObject.activeSelf)
{
_InputLogic = FriendAndMailRoot.Instance()._FriendRootLogic._ChatInputLogic;
}
_VoicePanel.SetActive(true);
_CancelPanel.SetActive(false);
ChatVoice.Instance().ChatVoiceStartRecord();
//ChatVoice.Instance().ChatVoiceStartRecord();
_Inputing = true;
}
public void StopInputVoice()
{
UIManager.CloseUI(UIInfo.ChatVoiceInputRoot);
Hashtable hash = new Hashtable();
hash.Add("Channel", (int)_ChatCannel);
ChatVoice.Instance().ChatVoiceFinishRecord(SendChatMsg, hash);
_Inputing = false;
}
public void CancelSlider()
{
_VoicePanel.SetActive(false);
_CancelPanel.SetActive(true);
}
public void CancelKeyUp()
{
UIManager.CloseUI(UIInfo.ChatVoiceInputRoot);
ChatVoice.Instance().ChatVoiceStopRecord();
}
public void UpdateVolumn()
{
if (!_Inputing)
return;
for (int i = 0; i < _VolumnGOs.Length; ++i)
{
if (i < ChatVoice.Instance()._VoiceVolume)
{
_VolumnGOs[i].SetActive(true);
}
else
{
_VolumnGOs[i].SetActive(false);
}
}
Debug.Log("UpdateVolumn:" + ChatVoice.Instance()._VoiceVolume);
}
#endregion
#region send msg
private static string _ChatMsg = "#[9,{0},{1},{2}]";
public static string _VoiceChatStart = "#[9";
public static char _VoiceChatEnd = ']';
public void SendChatMsg(string voiceUrl, string voiceText, float chatTime, string errorMsg, Hashtable hashParam)
{
int channel = (int)hashParam["Channel"];
LogModule.DebugLog("chatvoicelog:" + voiceText + " ,time:" + chatTime);
string chatText = voiceText.Trim(' ').Trim(',').Trim('');
chatText = chatText.Replace(",", "");
if (!string.IsNullOrEmpty(errorMsg))
{
GUIData.AddNotifyData(errorMsg);
return;
}
//没有错误,但是没有语音数据,提示权限错误
if (string.IsNullOrEmpty(voiceText))
{
GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{39050}"));
return;
}
//if (!ChatInputLogic.IsCanChat(channel, true))
// return;
//if (voiceUrl.Length + chatText.Length + 10 > _CharCnt)
//{
// chatText = chatText.Substring(0, _CharCnt - voiceUrl.Length - 10 - 3);
// chatText += "...";
//}
ChatHistoryItem item = new ChatHistoryItem();
item.CleanUp();
if ((GC_CHAT.CHATTYPE)channel == GC_CHAT.CHATTYPE.CHAT_TYPE_FRIEND)
{
item.EChannel = (GC_CHAT.CHATTYPE)channel;
item.ReceiverGuid = _InputLogic.FriendChatReceiverGuid;
item.ReceiverName = _InputLogic.FriendChatReceiverName;
}
else if ((GC_CHAT.CHATTYPE)channel != GC_CHAT.CHATTYPE.CHAT_TYPE_INVALID)
{
item.EChannel = (GC_CHAT.CHATTYPE)channel;
}
else
{
item.EChannel = (GC_CHAT.CHATTYPE)_InputLogic.GetCurChannelType();
item.ReceiverGuid = _InputLogic.FriendChatReceiverGuid;
item.ReceiverName = _InputLogic.FriendChatReceiverName;
}
item.ChatInfo = string.Format(_ChatMsg, voiceUrl, Mathf.CeilToInt(chatTime), chatText);
GuiTextDebug.debug("SendMsg:" + item.ChatInfo);
if (item.ReceiverGuid != GlobeVar.INVALID_GUID)
{
if (GameManager.gameManager.PlayerDataPool.ChatHistory.IsNeedInsertTime(item.ReceiverGuid))
{
item.SetSendMsgTime();
}
}
Utils.SendCGChatPak(item.ChatInfo, item);
}
#endregion
}