TestGetVoice.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  42. void Update()
  43. {
  44. //if(state)
  45. //times += Time.deltaTime;
  46. //if(times>5f)
  47. //{
  48. // times = 0;
  49. // bool success = _ajc.Call<bool>("StartRecognize");
  50. //}
  51. if(Initialize)
  52. {
  53. Initialize = false;
  54. try
  55. {
  56. Debug.unityLogger.logEnabled = true;
  57. // Debug.LogError("正在Start");
  58. text.text = "Start";
  59. Debug.unityLogger.logEnabled = true;
  60. //通过该API来实例化导入的arr中对应的类
  61. _ajc = new AndroidJavaObject("com.iflytek.mscv5plusdemo.AsrDemo");
  62. Debug.unityLogger.logEnabled = true;
  63. text.text = "实例化成功";
  64. // Debug.LogError(" 实例化成功");
  65. testVoice = transform.GetComponent<TestVoice>();
  66. bool success1 = _ajc.Call<bool>("InitAsr");
  67. if (true == success1)
  68. {
  69. Debug.unityLogger.logEnabled = true;
  70. text.text = "InitAsr成功";
  71. Debug.LogError(" InitAsr成功");
  72. //请求成功
  73. // log.text = "";
  74. bool success2 = _ajc.Call<bool>("Buildsyntax");
  75. if (true == success2)
  76. {
  77. Debug.unityLogger.logEnabled = true;
  78. text.text = "Buildsyntax成功";
  79. Debug.LogError(" Buildsyntax成功");
  80. //请求成功
  81. // log.text = "";
  82. bool success3 = _ajc.Call<bool>("StartRecognize");
  83. if (true == success3)
  84. {
  85. text.text = "Recongnize 等待返回值";
  86. Debug.LogError(" Recongnize 等待返回值");
  87. }
  88. else
  89. {
  90. text.text = "Recongnize 调用失败";
  91. Debug.LogError(" Recongnize 调用失败");
  92. }
  93. }
  94. }
  95. }
  96. catch (System.Exception e)
  97. {
  98. Debug.LogError($"Func Expection{e}");
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 场景上按点击时触发该方法
  104. /// </summary>
  105. public void OnBtnClick()
  106. {
  107. //通过API来调用原生代码的方法
  108. bool success = _ajc.Call<bool>("InitAsr");
  109. if (true == success)
  110. {
  111. //请求成功
  112. // log.text = "InitAsr成功";
  113. }
  114. }
  115. public void OnBtnClick2()
  116. {
  117. //通过API来调用原生代码的方法
  118. bool success = _ajc.Call<bool>("Buildsyntax");
  119. if (true == success)
  120. {
  121. //请求成功
  122. // log.text = "Buildsyntax成功";
  123. state = true;
  124. }
  125. }
  126. public void OnBtnClick3()
  127. {
  128. //通过API来调用原生代码的方法
  129. bool success = _ajc.Call<bool>("StartRecognize");
  130. if (true == success)
  131. {
  132. //请求成功
  133. // log.text = "开始识别";
  134. }
  135. }
  136. /// <summary>
  137. /// 原生层通过该方法传回信息
  138. /// </summary>
  139. /// <param name="content"></param>
  140. public void FromAndroid(string content)
  141. {
  142. Debug.Log(content);
  143. try
  144. {
  145. string result = System.Text.RegularExpressions.Regex.Replace(content, @"[^0-9]+", "");
  146. if (result != "")
  147. {
  148. float res = float.Parse(result);
  149. if (res > 60)
  150. testVoice.SettingVoice(AnalyzeResult(content));
  151. }
  152. Debug.Log(result);
  153. }
  154. catch (System.Exception)
  155. {
  156. throw;
  157. }
  158. if (content.Contains("Error") || content.Contains("结果"))
  159. {
  160. text.text = content;
  161. bool success = _ajc.Call<bool>("StartRecognize");
  162. }
  163. }
  164. private int AnalyzeResult(string str)
  165. {
  166. if ((str.Contains("进") || str.Contains("返回")) && str.Contains("全局"))
  167. {
  168. return 1;
  169. }
  170. else if ((str.Contains("进") || str.Contains("返回")) && str.Contains("临境"))
  171. {
  172. return 2;
  173. }
  174. else if ((str.Contains("进") || str.Contains("返回")) && str.Contains("协同"))
  175. {
  176. return 3;
  177. }
  178. else if (str.Contains("退出") && str.Contains("全局"))
  179. {
  180. return 4;
  181. }
  182. else if (str.Contains("退出") && str.Contains("临境"))
  183. {
  184. return 5;
  185. }
  186. else if (str.Contains("退出") && str.Contains("协同"))
  187. {
  188. return 6;
  189. }
  190. return 0;
  191. }
  192. }