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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| using System; using System.Collections; using System.Collections.Generic; using GCloud.GVoice; using UnityEngine;
public class TEST : MonoBehaviour {
private IGCloudVoiceEngine m_voiceEngine = null; // 这里的appId和appKey是测试用的,有使用限制 private const string appId = "1682684175"; private const string appKey = "ba613184a71174fcfdcdaabd132c7e6f"; private const string roomName = "solvarg";
private string showText = "ChatTest"; private Mode mode = Mode.RealTime;
private string m_recordPath = null; private string m_downloadPath = null;
private string m_fileId = null;
void DebugMessage(string msg) { Debug.LogError(msg); showText = msg; } void OnGUI() { if (GUI.Button(new Rect(300, 0, 100, 100), "加入房间")) { mode = Mode.RealTime; m_voiceEngine.JoinTeamRoom(roomName,10000);
}
if(GUI.Button(new Rect(0,0,100, 100), "开始录音")) { DebugMessage("开始录音"); StartRecord(); }
if(GUI.Button(new Rect(100,0,100,100), "结束录音")) { DebugMessage("结束录音"); StopRecord(); }
if(GUI.Button(new Rect(200,0,100,100),"切换录音模式")) { if (mode == Mode.Messages) mode = Mode.Translation; else mode = Mode.Messages;
m_voiceEngine.SetMode(mode); m_voiceEngine.ApplyMessageKey(15000); }
if(GUI.Button(new Rect(0,100, 100, 100),"开始播放")) { DebugMessage("开始播放"); PlayVoice(); }
if(GUI.Button(new Rect(100,100,100,100), "结束播放")) { DebugMessage("停止播放"); StopVoice(); }
GUI.TextField(new Rect(0, 200, 1080, 520), showText); }
// Start is called before the first frame update void Start() { if (m_voiceEngine == null) { m_voiceEngine = GCloudVoice.GetEngine(); TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0); string openId = System.Convert.ToInt64(ts.TotalSeconds).ToString(); m_voiceEngine.SetAppInfo(appId, appKey, openId); m_voiceEngine.Init();
m_voiceEngine.OnApplyMessageKeyCompleteEvent += OnApplyMessageKeyComplete; m_voiceEngine.OnUploadReccordFileCompleteEvent += OnUploadRecordFildComplete; m_voiceEngine.OnDownloadRecordFileCompleteEvent += OnDownloadRecordFileComplete; m_voiceEngine.OnPlayRecordFilCompleteEvent += OnPlayRecordFilComplete; m_voiceEngine.OnSpeechToTextEvent += OnSpeechToText; m_voiceEngine.OnJoinRoomCompleteEvent += OnJoinRoomComplete;
m_voiceEngine.SetMode(mode); m_voiceEngine.ApplyMessageKey(15000); }
m_recordPath = Application.persistentDataPath + "/" + "recording.dat"; m_downloadPath = Application.persistentDataPath + "/" + "download.dat"; }
private void OnJoinRoomComplete(CompleteCode code, string roomname, int memberid) { if (code == CompleteCode.JoinRoomSucc) { DebugMessage("加入 success!"); m_voiceEngine.OpenMic(); m_voiceEngine.OpenSpeaker(); } else { DebugMessage("加入失败 error: " + code); } }
// Update is called once per frame void Update() { if(m_voiceEngine != null) { m_voiceEngine.Poll(); } }
void OnApplyMessageKeyComplete(CompleteCode code) { if (code == CompleteCode.MessageKeyAppliedSucc) { DebugMessage("OnApplyMessageKeyComplete success!"); } else { DebugMessage("OnApplyMessageKeyComplete error: " + code); } }
void OnUploadRecordFildComplete(CompleteCode code, string filepath, string fileid) { if (code == CompleteCode.UploadRecordDone) { DebugMessage("OnUploadRecordFildComplete success! filepath: " + filepath); m_fileId = fileid; } else { DebugMessage("OnUploadRecordFildComplete error: " + code); } }
void OnDownloadRecordFileComplete(CompleteCode code, string filepath, string fileid) { if (code == CompleteCode.DownloadRecordDone) { DebugMessage("OnDownloadRecordFileComplete success! filepath: " + filepath); m_fileId = fileid; if (m_voiceEngine == null) return; if (mode == Mode.Messages) m_voiceEngine.PlayRecordedFile(m_downloadPath); else m_voiceEngine.SpeechToText(m_fileId); } else { DebugMessage("OnDownloadRecordFileComplete error: " + code); } }
void OnPlayRecordFilComplete(CompleteCode code, string filepath) { if (code == CompleteCode.PlayFileDone) { DebugMessage("OnUploadRecordFildComplete success! filepath: " + filepath); } else { DebugMessage("OnUploadRecordFildComplete error: " + code); } }
void OnSpeechToText(CompleteCode code, string fileID, string result) { if (code == CompleteCode.STTSucc) { DebugMessage("OnUploadRecordFildComplete success! result: " + result); } else { DebugMessage("OnUploadRecordFildComplete error: " + code); } }
void StartRecord() { if (m_voiceEngine == null) return; m_voiceEngine.StartRecording(m_recordPath); }
void StopRecord() { if (m_voiceEngine == null) return; m_voiceEngine.StopRecording(); m_voiceEngine.UploadRecordedFile(m_recordPath, 60000); }
void PlayVoice() { if (m_voiceEngine == null) return; m_voiceEngine.DownloadRecordedFile(m_fileId, m_downloadPath, 60000); }
private void OnApplicationQuit() { StopVoice(); }
void StopVoice() { if (m_voiceEngine == null) return; if(mode == Mode.Messages) m_voiceEngine.StopPlayFile(); if (mode == Mode.RealTime) { m_voiceEngine.CloseMic(); m_voiceEngine.QuitRoom(roomName,10000); m_voiceEngine.Pause(); } }
}
|