TestGetVoice.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class TestGetVoice : MonoBehaviour
  6. {
  7. public Text text;
  8. public bool Initialize = false;
  9. TestVoice testVoice;
  10. #region old
  11. //private TestVoice setVoice;
  12. //private UnityCallAndroid_Voice androidVoice;
  13. //float times = 0;
  14. //private void Start()
  15. //{
  16. // setVoice = transform.GetComponent<TestVoice>();
  17. // androidVoice = transform.GetComponent<UnityCallAndroid_Voice>();
  18. //}
  19. //private void Update()
  20. //{
  21. // times += Time.deltaTime;
  22. // if(times>1 && androidVoice.isStart)
  23. // {
  24. // times = 0;
  25. // text.text = " 获取中 ";
  26. // int recognition = androidVoice.GetRecognitions();
  27. // text.text = recognition.ToString();
  28. // Debug.Log("Recognition "+ recognition);
  29. // setVoice.SettingVoice(recognition);
  30. // }
  31. //}
  32. #endregion
  33. /// <summary>
  34. /// android原生代码对象
  35. /// </summary>
  36. AndroidJavaObject _ajc;
  37. bool state;
  38. float times = 0;
  39. void Start()
  40. {
  41. Initialize = true;
  42. }
  43. void Update()
  44. {
  45. //if(state)
  46. //times += Time.deltaTime;
  47. //if(times>5f)
  48. //{
  49. // times = 0;
  50. // bool success = _ajc.Call<bool>("StartRecognize");
  51. //}
  52. /*
  53. * 在定义数据类的时候,可以同时定义
  54. *
  55. */
  56. if(Initialize)
  57. {
  58. Initialize = false;
  59. try
  60. {
  61. // Debug.unityLogger.logEnabled = true;
  62. // Debug.LogError("正在Start");
  63. // text.text = "Start";
  64. // Debug.unityLogger.logEnabled = true;
  65. //通过该API来实例化导入的arr中对应的类
  66. _ajc = new AndroidJavaObject("com.iflytek.mscv5plusdemo.AsrDemo");
  67. // Debug.unityLogger.logEnabled = true;
  68. // text.text = "实例化成功";
  69. // Debug.LogError(" 实例化成功");
  70. testVoice = transform.GetComponent<TestVoice>();
  71. bool success1 = _ajc.Call<bool>("InitAsr");
  72. if (true == success1)
  73. {
  74. //Debug.unityLogger.logEnabled = true;
  75. //text.text = "InitAsr成功";
  76. // GameManager.Instance.text.text = "InitAsr成功";
  77. Debug.LogError(" InitAsr成功");
  78. //请求成功
  79. // log.text = "";
  80. bool success2 = _ajc.Call<bool>("Buildsyntax");
  81. if (true == success2)
  82. {
  83. Debug.unityLogger.logEnabled = true;
  84. text.text = "Buildsyntax成功";
  85. // GameManager.Instance.text2.text = "Buildsyntax成功";
  86. Debug.LogError(" Buildsyntax成功");
  87. //请求成功
  88. // log.text = "";
  89. bool success3 = _ajc.Call<bool>("StartRecognize");
  90. if (true == success3)
  91. {
  92. text.text = "Recongnize 等待返回值";
  93. // GameManager.Instance.text3.text = "Recongnize 等待返回值";
  94. Debug.LogError(" Recongnize 等待返回值");
  95. }
  96. else
  97. {
  98. text.text = "Recongnize 调用失败";
  99. // GameManager.Instance.text4.text = "Recongnize 调用失败";
  100. Debug.LogError(" Recongnize 调用失败");
  101. ErrorLogPanel.Instance.Show(" 语音识别系统调用失败 ");
  102. }
  103. }
  104. }
  105. }
  106. catch (System.Exception e)
  107. {
  108. ErrorLogPanel.Instance.Show(" 语音识别系统初始化失败 ");
  109. // GameManager.Instance.text.text = "语音识别系统初始化失败";
  110. Debug.LogError($"Func Expection{e}");
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 场景上按点击时触发该方法
  116. /// </summary>
  117. public void OnBtnClick()
  118. {
  119. //通过API来调用原生代码的方法
  120. bool success = _ajc.Call<bool>("InitAsr");
  121. if (true == success)
  122. {
  123. //请求成功
  124. // log.text = "InitAsr成功";
  125. }
  126. }
  127. public void OnBtnClick2()
  128. {
  129. //通过API来调用原生代码的方法
  130. bool success = _ajc.Call<bool>("Buildsyntax");
  131. if (true == success)
  132. {
  133. //请求成功
  134. // log.text = "Buildsyntax成功";
  135. state = true;
  136. }
  137. }
  138. public void OnBtnClick3()
  139. {
  140. //通过API来调用原生代码的方法
  141. bool success = _ajc.Call<bool>("StartRecognize");
  142. if (true == success)
  143. {
  144. //请求成功
  145. // log.text = "开始识别";
  146. }
  147. }
  148. /// <summary>
  149. /// 原生层通过该方法传回信息
  150. /// </summary>
  151. /// <param name="content"></param>
  152. public void FromAndroid(string content)
  153. {
  154. //Debug.Log(content);
  155. text.text = content;
  156. if(content.Contains("当前")||content.Contains("开始")||content.Contains("结束"))
  157. {
  158. }
  159. else
  160. {
  161. // GameManager.Instance.text2.text = content;
  162. }
  163. try
  164. {
  165. string result = System.Text.RegularExpressions.Regex.Replace(content, @"[^0-9]+", "");
  166. if (result != "")
  167. {
  168. float res = float.Parse(result);
  169. if (res > 50)
  170. testVoice.SettingVoice(AnalyzeResult(content));
  171. }
  172. // testVoice.SettingVoice(AnalyzeResult(content));
  173. // Debug.Log(result);
  174. }
  175. catch (System.Exception)
  176. {
  177. // onError Code: 11210
  178. throw;
  179. }
  180. if (content.Contains("Error") || content.Contains("结果"))
  181. {
  182. bool success = _ajc.Call<bool>("StartRecognize");
  183. // GameManager.Instance.text4.text = success.ToString();
  184. }
  185. }
  186. private int AnalyzeResult(string str)
  187. {
  188. // GameManager.Instance.text.text = str;
  189. if (str.Contains("进") || str.Contains("进入") || str.Contains("进入到") || str.Contains("打开"))
  190. {
  191. if (str.Contains("第一个景点"))
  192. {
  193. return 1;
  194. }
  195. else if (str.Contains("第二个景点"))
  196. {
  197. return 2;
  198. }
  199. else if (str.Contains("第三个景点"))
  200. {
  201. return 3;
  202. }
  203. else if (str.Contains("第四个景点"))
  204. {
  205. return 4;
  206. }
  207. else if (str.Contains("第五个景点"))
  208. {
  209. return 5;
  210. }
  211. }
  212. else if (str.Contains("音量"))
  213. {
  214. if (str.Contains("调高"))
  215. {
  216. return 11;
  217. }
  218. else if (str.Contains("调低"))
  219. {
  220. return 12;
  221. }
  222. }
  223. else if (str.Contains("返回") || str.Contains("返回到"))
  224. {
  225. if (str.Contains("第一个景点"))
  226. {
  227. return 1;
  228. }
  229. else if (str.Contains("第二个景点"))
  230. {
  231. return 2;
  232. }
  233. else if (str.Contains("第三个景点"))
  234. {
  235. return 3;
  236. }
  237. else if (str.Contains("第四个景点"))
  238. {
  239. return 4;
  240. }
  241. else if (str.Contains("第五个景点"))
  242. {
  243. return 5;
  244. }
  245. else if(str.Contains("菜单"))
  246. {
  247. return 13;
  248. }
  249. }
  250. return -1;
  251. }
  252. }