接过好几次SDK,发现每次再接都会忘,文档也写过,但也会找不到,就传网上,这样不会找不到了吧!
GVoiceSDK接入流程
1. 导入SDK文件:
将UnitySDK文件中的Plugins和Scripts导入到Unity项目中。
导入成功:
2. 导出Android项目:
将Unity切换到Android平台,导出项目
注意:不要忘记修改包名,例子:
导出成功后,会有2个文件夹:
3. 用eclipse导入项目:
导入之前生成的2个文件夹
4. 配置Android代码:
New一个Class,继承UnityPlayerActivity,取名为MainActivity,然后按官方文档书写代码,例子:
5. 生成Jar包:
选中MainActivity,右键Export,选择Jar file,next后接着选择生成目录,再点击finish就完成了。
注意:有可能生成不成功,报错,是因为步骤2生成的GCloudVoice文件夹中丢失jar包,可将UnitySDK文件中的Plugins\Android\GCloudVoice\libs路径下的GCloudVoice.jar拷贝到步骤2生成的GCloudVoice\bin路径下,再重新生成jar包
6. Jar包导入Unity:
将新生成的Jar包导入到Unity工程中的Assets\Plugins\Android下。
再将GVoiceDemo中的Assets\Plugins\Android路径下的AndroidManifest.xml导入到Unity工程中的Assets\Plugins\Android下:
接着修改AndroidManifest.xml
7. 开始测试GVoice代码
下面的代码实现了语音消息,和语音转文字功能。代码很简单,就不写注释了。
using UnityEngine;
using System.Collections;
using gcloud_voice;
using System;
public class ChatTest : MonoBehaviour
{
private IGCloudVoice m_voiceEngine = null;
// 这里的appId和appKey是测试用的,有使用限制
private const string appId = "1682684175";
private const string appKey = "ba613184a71174fcfdcdaabd132c7e6f";
private string showText = "ChatTest";
private GCloudVoiceMode mode = GCloudVoiceMode.Messages;
private string m_recordPath = null;
private string m_downloadPath = null;
private string m_fileId = null;
void OnGUI()
{
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 == GCloudVoiceMode.Messages)
mode = GCloudVoiceMode.Translation;
else
mode = GCloudVoiceMode.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);
}
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.OnApplyMessageKeyComplete += OnApplyMessageKeyComplete;
m_voiceEngine.OnUploadReccordFileComplete += OnUploadRecordFildComplete;
m_voiceEngine.OnDownloadRecordFileComplete += OnDownloadRecordFileComplete;
m_voiceEngine.OnPlayRecordFilComplete += OnPlayRecordFilComplete;
m_voiceEngine.OnSpeechToText += OnSpeechToText;
m_voiceEngine.SetMode(mode);
m_voiceEngine.ApplyMessageKey(15000);
}
m_recordPath = Application.persistentDataPath + "/" + "recording.dat";
m_downloadPath = Application.persistentDataPath + "/" + "download.dat";
}
void Update()
{
if(m_voiceEngine != null)
{
m_voiceEngine.Poll();
}
}
void OnApplyMessageKeyComplete(IGCloudVoice.GCloudVoiceCompleteCode code)
{
if (code == IGCloudVoice.GCloudVoiceCompleteCode.GV_ON_MESSAGE_KEY_APPLIED_SUCC)
{
DebugMessage("OnApplyMessageKeyComplete success!");
}
else
{
DebugMessage("OnApplyMessageKeyComplete error: " + code);
}
}
void OnUploadRecordFildComplete(IGCloudVoice.GCloudVoiceCompleteCode code, string filepath, string fileid)
{
if (code == IGCloudVoice.GCloudVoiceCompleteCode.GV_ON_UPLOAD_RECORD_DONE)
{
DebugMessage("OnUploadRecordFildComplete success! filepath: " + filepath);
m_fileId = fileid;
}
else
{
DebugMessage("OnUploadRecordFildComplete error: " + code);
}
}
void OnDownloadRecordFileComplete(IGCloudVoice.GCloudVoiceCompleteCode code, string filepath, string fileid)
{
if (code == IGCloudVoice.GCloudVoiceCompleteCode.GV_ON_DOWNLOAD_RECORD_DONE)
{
DebugMessage("OnDownloadRecordFileComplete success! filepath: " + filepath);
m_fileId = fileid;
if (m_voiceEngine == null) return;
if (mode == GCloudVoiceMode.Messages)
m_voiceEngine.PlayRecordedFile(m_downloadPath);
else
m_voiceEngine.SpeechToText(m_fileId);
}
else
{
DebugMessage("OnDownloadRecordFileComplete error: " + code);
}
}
void OnPlayRecordFilComplete(IGCloudVoice.GCloudVoiceCompleteCode code, string filepath)
{
if (code == IGCloudVoice.GCloudVoiceCompleteCode.GV_ON_PLAYFILE_DONE)
{
DebugMessage("OnUploadRecordFildComplete success! filepath: " + filepath);
}
else
{
DebugMessage("OnUploadRecordFildComplete error: " + code);
}
}
void OnSpeechToText(IGCloudVoice.GCloudVoiceCompleteCode code, string fileID, string result)
{
if (code == IGCloudVoice.GCloudVoiceCompleteCode.GV_ON_STT_SUCC)
{
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);
}
void StopVoice()
{
if (m_voiceEngine == null) return;
if(mode == GCloudVoiceMode.Messages)
m_voiceEngine.StopPlayFile();
}
void DebugMessage(string msg)
{
Debug.LogError(msg);
showText = msg;
}
}