using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestGetVoice : MonoBehaviour { public Text text; public bool Initialize = false; TestVoice testVoice; #region old //private TestVoice setVoice; //private UnityCallAndroid_Voice androidVoice; //float times = 0; //private void Start() //{ // setVoice = transform.GetComponent(); // androidVoice = transform.GetComponent(); //} //private void Update() //{ // times += Time.deltaTime; // if(times>1 && androidVoice.isStart) // { // times = 0; // text.text = " 获取中 "; // int recognition = androidVoice.GetRecognitions(); // text.text = recognition.ToString(); // Debug.Log("Recognition "+ recognition); // setVoice.SettingVoice(recognition); // } //} #endregion /// /// android原生代码对象 /// AndroidJavaObject _ajc; bool state; float times = 0; void Start() { Initialize = true; } void Update() { //if(state) //times += Time.deltaTime; //if(times>5f) //{ // times = 0; // bool success = _ajc.Call("StartRecognize"); //} /* * 在定义数据类的时候,可以同时定义 * */ if(Initialize) { Initialize = false; try { // Debug.unityLogger.logEnabled = true; // Debug.LogError("正在Start"); // text.text = "Start"; // Debug.unityLogger.logEnabled = true; //通过该API来实例化导入的arr中对应的类 _ajc = new AndroidJavaObject("com.iflytek.mscv5plusdemo.AsrDemo"); // Debug.unityLogger.logEnabled = true; // text.text = "实例化成功"; // Debug.LogError(" 实例化成功"); testVoice = transform.GetComponent(); bool success1 = _ajc.Call("InitAsr"); if (true == success1) { //Debug.unityLogger.logEnabled = true; //text.text = "InitAsr成功"; // GameManager.Instance.text.text = "InitAsr成功"; Debug.LogError(" InitAsr成功"); //请求成功 // log.text = ""; bool success2 = _ajc.Call("Buildsyntax"); if (true == success2) { Debug.unityLogger.logEnabled = true; text.text = "Buildsyntax成功"; // GameManager.Instance.text2.text = "Buildsyntax成功"; Debug.LogError(" Buildsyntax成功"); //请求成功 // log.text = ""; bool success3 = _ajc.Call("StartRecognize"); if (true == success3) { text.text = "Recongnize 等待返回值"; // GameManager.Instance.text3.text = "Recongnize 等待返回值"; Debug.LogError(" Recongnize 等待返回值"); } else { text.text = "Recongnize 调用失败"; // GameManager.Instance.text4.text = "Recongnize 调用失败"; Debug.LogError(" Recongnize 调用失败"); ErrorLogPanel.Instance.Show(" 语音识别系统调用失败 "); } } } } catch (System.Exception e) { ErrorLogPanel.Instance.Show(" 语音识别系统初始化失败 "); // GameManager.Instance.text.text = "语音识别系统初始化失败"; Debug.LogError($"Func Expection{e}"); } } } /// /// 场景上按点击时触发该方法 /// public void OnBtnClick() { //通过API来调用原生代码的方法 bool success = _ajc.Call("InitAsr"); if (true == success) { //请求成功 // log.text = "InitAsr成功"; } } public void OnBtnClick2() { //通过API来调用原生代码的方法 bool success = _ajc.Call("Buildsyntax"); if (true == success) { //请求成功 // log.text = "Buildsyntax成功"; state = true; } } public void OnBtnClick3() { //通过API来调用原生代码的方法 bool success = _ajc.Call("StartRecognize"); if (true == success) { //请求成功 // log.text = "开始识别"; } } /// /// 原生层通过该方法传回信息 /// /// public void FromAndroid(string content) { //Debug.Log(content); text.text = content; if(content.Contains("当前")||content.Contains("开始")||content.Contains("结束")) { } else { // GameManager.Instance.text2.text = content; } try { string result = System.Text.RegularExpressions.Regex.Replace(content, @"[^0-9]+", ""); if (result != "") { float res = float.Parse(result); if (res > 50) testVoice.SettingVoice(AnalyzeResult(content)); } // testVoice.SettingVoice(AnalyzeResult(content)); // Debug.Log(result); } catch (System.Exception) { // onError Code: 11210 throw; } if (content.Contains("Error") || content.Contains("结果")) { bool success = _ajc.Call("StartRecognize"); // GameManager.Instance.text4.text = success.ToString(); } } private int AnalyzeResult(string str) { // GameManager.Instance.text.text = str; if (str.Contains("进") || str.Contains("进入") || str.Contains("进入到") || str.Contains("打开")) { if (str.Contains("第一个景点")) { return 1; } else if (str.Contains("第二个景点")) { return 2; } else if (str.Contains("第三个景点")) { return 3; } else if (str.Contains("第四个景点")) { return 4; } else if (str.Contains("第五个景点")) { return 5; } } else if (str.Contains("音量")) { if (str.Contains("调高")) { return 11; } else if (str.Contains("调低")) { return 12; } } else if (str.Contains("返回") || str.Contains("返回到")) { if (str.Contains("第一个景点")) { return 1; } else if (str.Contains("第二个景点")) { return 2; } else if (str.Contains("第三个景点")) { return 3; } else if (str.Contains("第四个景点")) { return 4; } else if (str.Contains("第五个景点")) { return 5; } else if(str.Contains("菜单")) { return 13; } } return -1; } }