123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- 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<TestVoice>();
- // androidVoice = transform.GetComponent<UnityCallAndroid_Voice>();
- //}
- //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
- /// <summary>
- /// android原生代码对象
- /// </summary>
- 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<bool>("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<TestVoice>();
- bool success1 = _ajc.Call<bool>("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<bool>("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<bool>("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}");
- }
- }
- }
- /// <summary>
- /// 场景上按点击时触发该方法
- /// </summary>
- public void OnBtnClick()
- {
- //通过API来调用原生代码的方法
- bool success = _ajc.Call<bool>("InitAsr");
- if (true == success)
- {
- //请求成功
- // log.text = "InitAsr成功";
- }
- }
- public void OnBtnClick2()
- {
- //通过API来调用原生代码的方法
- bool success = _ajc.Call<bool>("Buildsyntax");
- if (true == success)
- {
- //请求成功
- // log.text = "Buildsyntax成功";
- state = true;
- }
- }
- public void OnBtnClick3()
- {
- //通过API来调用原生代码的方法
- bool success = _ajc.Call<bool>("StartRecognize");
- if (true == success)
- {
- //请求成功
- // log.text = "开始识别";
- }
- }
- /// <summary>
- /// 原生层通过该方法传回信息
- /// </summary>
- /// <param name="content"></param>
- 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<bool>("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;
- }
- }
|