using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.EventSystems;
|
using System;
|
using vnxbqy.UI;
|
using UnityEngine.Android;
|
|
public class VoiceRecorder : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
{
|
[SerializeField] RectTransform m_ContainerArea;
|
|
int samplesOfRecoding = 0;
|
DateTime overTime = DateTime.Now;
|
bool recoding = false;
|
|
DateTime startRecordTime = DateTime.Now;
|
|
Coroutine cacheCoroutine;
|
|
public event Action<int, float> voiceRecordSuccessEvent;
|
|
[SerializeField] AudioSource m_AudioSource;
|
|
private void OnEnable()
|
{
|
recoding = false;
|
m_AudioSource.loop = false;
|
m_AudioSource.mute = true;
|
}
|
|
private void OnDisable()
|
{
|
StopRecord();
|
}
|
|
//检查是否有录音权限
|
public bool HasRecordAudioPermission()
|
{
|
if (Application.platform == RuntimePlatform.Android)
|
{
|
if (Permission.HasUserAuthorizedPermission(AndroidPermissions.RECORD_AUDIO))
|
return true;
|
else
|
{
|
ynmbxxjUtil.Instance.RequestAndroidPermission(AndroidPermissions.RECORD_AUDIO, (per, state) =>
|
{
|
if (per == AndroidPermissions.RECORD_AUDIO && state == ynmbxxjUtil.CallBackState.Error)
|
//权限申请被拒绝,并且不再提示
|
SysNotifyMgr.Instance.ShowTip("NoPerRecordAudio");
|
});
|
return false;
|
}
|
}
|
return true;
|
}
|
|
public void OnPointerDown(PointerEventData eventData)
|
{
|
if (SpeechTranslate.Instance.busy || VoiceCodec.IsBusy)
|
{
|
return;
|
}
|
|
if ((ChatCtrl.Inst.presentChatType == ChatInfoType.Friend
|
&& ChatCtrl.Inst.PteChatID == 0) || ChatCtrl.Inst.presentChatType == ChatInfoType.Trumpet)
|
{
|
return;
|
}
|
if (!HasRecordAudioPermission())
|
return;
|
overTime = DateTime.Now + new TimeSpan(SpeechTranslate.AUDIO_TIMELIMIT * TimeSpan.TicksPerSecond);
|
AudioListener.volume = 0f;
|
cacheCoroutine = StartCoroutine(Co_StartRecord());
|
}
|
|
public void OnPointerUp(PointerEventData eventData)
|
{
|
if (recoding)
|
{
|
StopRecord();
|
|
var worldposition = CameraManager.uiCamera.ScreenToWorldPoint(Input.mousePosition);
|
var checkRect = m_ContainerArea == null ? (transform as RectTransform) : m_ContainerArea;
|
if (!m_ContainerArea.ContainWorldPosition(worldposition))
|
{
|
SysNotifyMgr.Instance.ShowTip("VoiceSendFail");
|
return;
|
}
|
|
var seconds = (DateTime.Now - startRecordTime).TotalSeconds;
|
|
var voiceInstanceId = VoiceWarehouse.SaveVoice(m_AudioSource.clip);
|
if (voiceRecordSuccessEvent != null)
|
{
|
voiceRecordSuccessEvent(voiceInstanceId, (float)seconds);
|
}
|
VoiceTranslate(voiceInstanceId, samplesOfRecoding);
|
}
|
else if (cacheCoroutine != null)
|
{
|
StopCoroutine(cacheCoroutine);
|
cacheCoroutine = null;
|
WindowCenter.Instance.Close<MonitorVoiceWin>();
|
AudioListener.volume = 1f;
|
}
|
}
|
|
private void LateUpdate()
|
{
|
if (DateTime.Now > overTime)
|
{
|
if (recoding)
|
{
|
StopRecord();
|
var voiceInstanceId = VoiceWarehouse.SaveVoice(m_AudioSource.clip);
|
if (voiceRecordSuccessEvent != null)
|
{
|
var seconds = (DateTime.Now - startRecordTime).TotalSeconds;
|
voiceRecordSuccessEvent(voiceInstanceId, (float)seconds);
|
}
|
VoiceTranslate(voiceInstanceId, samplesOfRecoding);
|
}
|
}
|
}
|
|
private void StartRecord()
|
{
|
if (Microphone.devices == null || Microphone.devices.Length == 0)
|
{
|
DebugEx.Log("请插入麦克风。");
|
return;
|
}
|
|
if (!WindowCenter.Instance.IsOpen<MonitorVoiceWin>())
|
{
|
MonitorVoiceWin.audioSource = null;
|
WindowCenter.Instance.Open<MonitorVoiceWin>(true);
|
}
|
|
recoding = true;
|
m_AudioSource.clip = Microphone.Start(null, false, SpeechTranslate.AUDIO_TIMELIMIT, VoiceSettings.frequency);
|
startRecordTime = DateTime.Now;
|
m_AudioSource.Play();
|
MonitorVoiceWin.audioSource = m_AudioSource;
|
}
|
|
IEnumerator Co_StartRecord()
|
{
|
yield return WaitingForSecondConst.WaitMS300;
|
StartRecord();
|
}
|
|
private void StopRecord()
|
{
|
recoding = false;
|
AudioListener.volume = 1f;
|
|
if (cacheCoroutine != null)
|
{
|
StopCoroutine(cacheCoroutine);
|
cacheCoroutine = null;
|
}
|
|
WindowCenter.Instance.Close<MonitorVoiceWin>();
|
|
if (Microphone.devices == null || Microphone.devices.Length == 0)
|
{
|
DebugEx.Log("请插入麦克风。");
|
return;
|
}
|
|
if (!Microphone.IsRecording(null))
|
{
|
return;
|
}
|
|
samplesOfRecoding = Microphone.GetPosition(null);
|
Microphone.End(null);
|
m_AudioSource.Stop();
|
}
|
|
private void PlayAudio()
|
{
|
if (!m_AudioSource.isPlaying)
|
{
|
m_AudioSource.Play();
|
}
|
}
|
|
private void VoiceTranslate(int _instanceId, int _samples)
|
{
|
AudioClip clip;
|
if (VoiceWarehouse.TryGetVoice(_instanceId, out clip))
|
{
|
var data = SpeechTranslate.AudioCompress(clip, Mathf.CeilToInt(_samples / (float)VoiceSettings.frequency));
|
SpeechTranslate.Instance.ReqeustVoiceTranslate(_instanceId, data);
|
SpeechTranslate.Instance.AudioCompress(_instanceId, clip);
|
}
|
}
|
|
}
|