少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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);
        }
    }
 
}