客户端调用Android端Java的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AndroidBtn : MonoBehaviour
{
public GameObject prefab;
public Transform content;
//开始录音
public void OnBtnOpen()
{
AndroidJavaClass aj = new AndroidJavaClass("com.unity3d.player.UnityPlayerActivity");
aj.CallStatic("OnBtnOpen");
}
//结束录音
public void OnBtnStop()
{
AndroidJavaClass aj = new AndroidJavaClass("com.unity3d.player.UnityPlayerActivity");
aj.CallStatic("OnBtnStop");
}
//接收语音识别返回
public void SetText(string str)
{
GameObject chat = Instantiate(prefab, content);
chat.GetComponent<Text>().text = str;
}
}
腾讯云开通账号
public static final String apppId = "xxx";
public static final String secretId = "xxx";
public static final String secretKey = "xxx";
Android端接入SDK代码
注意
<service android:name="com.tencent.cloud.qcloudasrsdk.onesentence.recorder.QCloudAudioMp3RecoderService" />
需要放在application标签内
初始化接口
private static QCloudOneSentenceRecognizer recognizer;
// Setup activity layout
@Override protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
String cmdLine = updateUnityCommandLineArguments(getIntent().getStringExtra("unity"));
getIntent().putExtra("unity", cmdLine);
mUnityPlayer = new UnityPlayer(this, this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
recognizer = new QCloudOneSentenceRecognizer(this, apppId, secretId, secretKey);
recognizer.setCallback(this);
}
开始录音
public static void OnBtnOpen(){
System.out.println("开始录音");
try {
/**
* setDefaultParams 默认参数param
* @param filterDirty 0 :默认状态 不过滤脏话 1:过滤脏话
* @param filterModal 0 :默认状态 不过滤语气词 1:过滤部分语气词 2:严格过滤
* @param filterPunc 0 :默认状态 不过滤句末的句号 1:滤句末的句号
* @param convertNumMode 1:默认状态 根据场景智能转换为阿拉伯数字;0:全部转为中文数字。
* @param hotwordId 热词id,不使用则传null
* @param engSerViceType 引擎模型类型,传null默认使用“16k_zh”
*/
recognizer.setDefaultParams(0, 0, 1, 1,null,null);
recognizer.getDefaultParams().setReinforceHotword(1); // 开启热词增强
recognizer.recognizeWithRecorder();
recognizer.setQCloudOneSentenceRecognizerAudioPathListener(new QCloudOneSentenceRecognizerAudioPathListener() {
@Override
public void callBackAudioPath(String audioPath) {
System.out.println("callBackAudioPath: audioPath="+audioPath);
}
});
} catch (Exception e) {
e.printStackTrace();
System.out.println("exception msg" + e.getMessage());
} finally {
}
}
结束录音
public static void OnBtnStop(){
System.out.println("结束录音");
recognizer.stopRecognizeWithRecorder();
}
回调继承
public class UnityPlayerActivity extends Activity implements IUnityPlayerLifecycleEvents, QCloudOneSentenceRecognizerListener
语音识别回调函数
@Override
public void didStartRecord() {
System.out.println("开始录音回调");
}
@Override
public void didStopRecord() {
System.out.println("结束录音回调");
}
@Override
public void recognizeResult(QCloudOneSentenceRecognizer recognizer, String result, Exception exception) {
if (exception != null) {
System.out.println("语音识别错误:" + exception.getLocalizedMessage());
UnityPlayer.UnitySendMessage("Button","SetText",exception.getLocalizedMessage());
}
else {
System.out.println("语音识别成功:" + result);
//返回的是json文件,需要解析
JsonParser parse = new JsonParser();
JsonObject json = (JsonObject) parse.parse(result);
JsonObject response = json.getAsJsonObject("Response");
String str = response.get("Result").getAsString();
UnityPlayer.UnitySendMessage("Button","SetText",str);
}
}