https://www.cnblogs.com/davidsheh/p/7410464.html https://www.zhihu.com/question/56416062

# GVoice 集成到 Unity 中

GVoice 官网 可以在上方找到对应 SDK 的接口文档.
这个插件如果要导出到安卓层,则需要从安卓层来接。接下来介绍如何从安卓层接 GVoice 插件.(顺带学习如何在 Android 层接 SDK)

# 下载 GVoice SDK

在这个页面可以选择对应平台的接口,比如 UE,Unity http://sdk.gcloud.tencent.com/ 这里我们选择 Unity2019.3+
下载完后文件夹内会有如下文件: GCloudSDK: 对应于 Unity 平台的 SDK
Plugins: 对应于导出到 Android 所需要的 AndroidManifest 文件模板和安卓启动入口类 MainActivity 模板. (注意,要和原项目整合到一起的话需要把MainActivity里的文件添加到原项目的入口文件内.而不能直接切换入口文件) 只需要把上面两个文件夹导入到项目中即可。然后在一个新场景下编写如下代码:

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();
}
}

}

然后需要注意的是,在 Editor 的 Android 平台下是无法调试的 (暂不知道为啥,必须打包到真机测试).

# 打包,接 SDK

BuildSettings 中勾选 Export Project 然后点击 Export Export 完毕后会生成一个 Android Studio 的项目,这里我们可以使用 Android Studio 打开这个项目. 然后会报一大堆错误 Maven 的仓库地址改一下:

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
allprojects {
buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'

}
}

repositories {
mavenCentral()
google()
jcenter()
maven {
url 'https://csspeechstorage.blob.core.windows.net/maven/'
}
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

然后把 MainActivity 中的内容添加到原项目的中 生成后 Rebuild 即可