客戶端調(diào)用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");
}
//結(jié)束錄音
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標(biāo)簽內(nèi)
初始化接口
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 默認(rèn)參數(shù)param
* @param filterDirty 0 :默認(rèn)狀態(tài) 不過濾臟話 1:過濾臟話
* @param filterModal 0 :默認(rèn)狀態(tài) 不過濾語氣詞 1:過濾部分語氣詞 2:嚴(yán)格過濾
* @param filterPunc 0 :默認(rèn)狀態(tài) 不過濾句末的句號 1:濾句末的句號
* @param convertNumMode 1:默認(rèn)狀態(tài) 根據(jù)場景智能轉(zhuǎn)換為阿拉伯?dāng)?shù)字;0:全部轉(zhuǎn)為中文數(shù)字。
* @param hotwordId 熱詞id,不使用則傳null
* @param engSerViceType 引擎模型類型,傳null默認(rèn)使用“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 {
}
}
結(jié)束錄音
public static void OnBtnStop(){
System.out.println("結(jié)束錄音");
recognizer.stopRecognizeWithRecorder();
}
回調(diào)繼承
public class UnityPlayerActivity extends Activity implements IUnityPlayerLifecycleEvents, QCloudOneSentenceRecognizerListener
語音識別回調(diào)函數(shù)
@Override
public void didStartRecord() {
System.out.println("開始錄音回調(diào)");
}
@Override
public void didStopRecord() {
System.out.println("結(jié)束錄音回調(diào)");
}
@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);
}
}