ERNIEBotManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using static TestFile;
  6. using UnityEngine.Android;
  7. public class ERNIEBotManager : yiyan.MonoSingleton<ERNIEBotManager>
  8. {
  9. public bool isInit;
  10. public Text backt;
  11. public Text backfinalt;
  12. public Text myt;
  13. public RectTransform scrollRect;
  14. public GameObject ui;
  15. public Transform Irobot;
  16. // private Animator animator;
  17. public Transform player;
  18. private bool isCollect;
  19. private float times;
  20. private float times2;
  21. private float times3;
  22. private float backtTimes;
  23. private string oldMyt;
  24. private string oldback;
  25. private XunFeiYuYin xunfei;
  26. private float interval;
  27. private const string RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
  28. private bool hasRecordAudioPermission = false;
  29. private bool isWakeup = false;
  30. private Vector3 irobotPos;
  31. public bool isAudioPlay;
  32. private void Awake()
  33. {
  34. //animator = Irobot.GetComponent<Animator>();
  35. }
  36. private void OnEnable()
  37. {
  38. // transform.LookAt(API_GSXR_Slam.GSXR_Get_Head());
  39. // StartCoroutine(PlayAnimator("Waving", 1f));
  40. }
  41. void Start()
  42. {
  43. if (player == null)
  44. {
  45. player = OpenXRCamera.Instance.head.transform;
  46. }
  47. // 检查是否已经授权了麦克风权限
  48. hasRecordAudioPermission = Permission.HasUserAuthorizedPermission(RECORD_AUDIO_PERMISSION);
  49. if (!hasRecordAudioPermission)
  50. {
  51. // 请求麦克风权限
  52. Permission.RequestUserPermission(RECORD_AUDIO_PERMISSION);
  53. }
  54. isInit = true;
  55. isCollect = false;
  56. times = 0;
  57. oldMyt = null;
  58. oldback = null;
  59. backtTimes = 0;
  60. interval = 0.05f;
  61. irobotPos = new Vector3(0, -1f, 4f);
  62. //HttpTool.Instance.Get("https://fat1.qulivr.com/mr-navigator/v1/", "", (string getInterval) => {
  63. // Debug.Log(getInterval);
  64. // if (getInterval == null)
  65. // Debug.LogError(" HttpTool Get Interval Error!!! ");
  66. // JsonData data = JsonMapper.ToObject(getInterval);
  67. // Debug.Log(data["code"]);
  68. // if ((int)data["code"] == 200)
  69. // {
  70. // Debug.Log(data["rate"].ToString());
  71. // interval = float.Parse(data["rate"].ToString());
  72. // float z = float.Parse(data["defaultZAxisValue"].ToString());
  73. // float y = float.Parse(data["defaultHeight"].ToString());
  74. // z = (string.IsNullOrEmpty(data["defaultZAxisValue"].ToString())) ? 1 : z;
  75. // y = (string.IsNullOrEmpty(data["defaultHeight"].ToString())) ? 2 : y;
  76. // irobotPos = new Vector3(0, y, z);
  77. // }
  78. // else
  79. // Debug.LogError(" HttpTool Get Interval Error!!! ");
  80. //});
  81. YiYanBack _setAndrodCallback = new YiYanBack();
  82. #if UNITY_ANDROID
  83. UnityToJava.initSpeek(_setAndrodCallback);
  84. isInit = true;
  85. isAudioPlay = false;
  86. xunfei = XunFeiYuYin.Init("708f4bb8", "MTQ0ZWZhMDE1ODA5NjVmMDYyODhmMTM3", "e2c01e5127d59a6ef92cef37e6255644", "");
  87. #endif
  88. StartCoroutine(OpenSpeek(3f));
  89. }
  90. private IEnumerator Speek(float times)
  91. {
  92. yield return new WaitForSeconds(times);
  93. // UnityToJava.yiYanClass.CallStatic("SpeekOff", "HelloWord1234568999");
  94. }
  95. private void Update()
  96. {
  97. backtTimes += Time.deltaTime;
  98. if(backtTimes> interval && YiYanBack.BackText.Count>0)
  99. {
  100. char str = YiYanBack.BackText.Dequeue();
  101. backt.text += str;
  102. backtTimes = 0;
  103. times2 = 0;
  104. oldback = backt.text;
  105. // xunfei.startQueue(str.ToString());
  106. }
  107. if (YiYanBack.BackTextlist.Count > 0)
  108. {
  109. //backt.text += YiYanBack.BackTextlist.Dequeue();
  110. // YiYanBack.BackTextlist.Dequeue();
  111. xunfei.startQueue(YiYanBack.BackTextlist.Dequeue());
  112. //times2 = 0;
  113. //Debug.Log(backt.text);
  114. //oldback = backt.text;
  115. // StartCoroutine(PlayAnimator("Point_left", 1f));
  116. }
  117. if (YiYanBack.BackFINALTextlist.Count > 0)
  118. {
  119. backfinalt.text = YiYanBack.BackFINALTextlist.Dequeue();
  120. send(backfinalt.text);
  121. }
  122. if (YiYanBack.MyTextlist.Count > 0)
  123. {
  124. myt.text = YiYanBack.MyTextlist.Dequeue();
  125. oldMyt = myt.text;
  126. //HttpsSendLog.Instance.SendLog("MYT", myt.text);
  127. if (myt.text.Contains("唤醒成功") && myt.text.Contains("小度小度"))
  128. {
  129. myt.text = "";
  130. oldMyt = "";
  131. if (!Irobot.gameObject.activeSelf)
  132. {
  133. transform.position = player.position + player.forward * irobotPos.z;
  134. transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
  135. transform.LookAt(player);
  136. transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  137. Irobot.gameObject.SetActive(true);
  138. }
  139. if (backt.text != "")
  140. {
  141. xunfei.stop();
  142. }
  143. isWakeup = true;
  144. StartCoroutine(AudioIconSetting(0, true));
  145. YiYanBack.BackText.Clear();
  146. YiYanBack.BackText = new Queue<char>();
  147. YiYanBack.BackTextlist.Clear();
  148. YiYanBack.BackTextlist = new Queue<string>();
  149. // backt.text = "";
  150. // StartSpeek();
  151. }
  152. else
  153. isWakeup = false;
  154. times2 = 0;
  155. times3 = 0;
  156. backt.text = "";
  157. // HttpsSendLog.Instance.SendLog("MYT", oldMyt+" MY "+ myt.text);
  158. }
  159. if (oldback != null && oldback == backt.text && !isAudioPlay)
  160. {
  161. times2 += Time.deltaTime;
  162. if (times2 > 8f)
  163. {
  164. times2 = 0;
  165. isWakeup = false;
  166. Irobot.gameObject.SetActive(false);
  167. }
  168. }
  169. else
  170. times2 = 0;
  171. if (oldMyt != null && myt.text == oldMyt)
  172. {
  173. // HttpsSendLog.Instance.SendLog("MYT", " 计时开始 " + times);
  174. times += Time.deltaTime;
  175. if (times > 3f)
  176. {
  177. send(myt.text);
  178. oldMyt = null;
  179. times = 0;
  180. }
  181. }
  182. if (isWakeup)
  183. {
  184. times3 += Time.deltaTime;
  185. if (times3 > 10f)
  186. {
  187. times3 = 0;
  188. isWakeup = false;
  189. Irobot.gameObject.SetActive(false);
  190. }
  191. }
  192. #region old
  193. /*
  194. //if (oldback != null && oldback == backt.text)
  195. //{
  196. // times2 += Time.deltaTime;
  197. // if (times2 > 3f)
  198. // {
  199. // oldback = null;
  200. // times2 = 0;
  201. // StartSpeek();
  202. // // StartCoroutine(OpenSpeek(1f));
  203. // }
  204. //}
  205. //else
  206. // times2 = 0;
  207. //if (oldMyt!=null && myt.text == oldMyt)
  208. //{
  209. // if(!isWakeup&&myt.text.Contains("小度小度"))
  210. // {
  211. // isWakeup = true;
  212. // transform.position = player.position + player.forward * irobotPos.z;
  213. // transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
  214. // transform.LookAt(player);
  215. // transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
  216. // Irobot.gameObject.SetActive(true);
  217. // myt.text = "";
  218. // backfinalt.text = "";
  219. // backt.text = "";
  220. // // StartCoroutine(OpenSpeek(0.3f));
  221. // StartSpeek();
  222. // }
  223. // times += Time.deltaTime;
  224. // if(times>1f)
  225. // StartCoroutine(AudioIconSetting(0, false));
  226. // if (times > 3f)
  227. // {
  228. // oldMyt = null;
  229. // times = 0;
  230. // send(myt.text);
  231. // }
  232. //}else
  233. //{
  234. // times = 0;
  235. //}
  236. */
  237. #endregion
  238. }
  239. private void OnApplicationPause(bool pause)
  240. {
  241. if (isInit)
  242. {
  243. if (pause)
  244. {
  245. UnityToJava.yiYanClass.CallStatic("onPauseJava");
  246. }
  247. else
  248. {
  249. UnityToJava.yiYanClass.CallStatic("onResumeJava");
  250. }
  251. }
  252. }
  253. private void OnDestroy()
  254. {
  255. UnityToJava.yiYanClass.CallStatic("onDestroyJava");
  256. }
  257. public void StopSpeek()
  258. {
  259. UnityToJava.yiYanClass.CallStatic("StopSpeek");
  260. Debug.Log("StopSpeek====");
  261. }
  262. public void StartSpeekOnClick()
  263. {
  264. StartSpeek();
  265. YiYanBack.BackText.Clear();
  266. YiYanBack.BackText = new Queue<char>();
  267. myt.text = "";
  268. backt.text = "";
  269. }
  270. public void StartSpeek()
  271. {
  272. UnityToJava.yiYanClass.CallStatic("StartSpeek");
  273. //Debug.Log("StartSpeek====");
  274. //StartCoroutine(AudioIconSetting(0.7f, true));
  275. }
  276. public void send(string str)
  277. {
  278. //StartCoroutine(AudioIconSetting(0, false));
  279. //if (isWakeup)
  280. //{
  281. // if (myt.text.Contains("小度拜拜"))
  282. // {
  283. // isWakeup = false;
  284. // Irobot.gameObject.SetActive(false);
  285. // StartSpeek();
  286. // }
  287. // else
  288. // {
  289. // UnityToJava.yiYanClass.CallStatic("SendText", str);
  290. // Debug.Log("Send " + str);
  291. // }
  292. //}
  293. //else
  294. //{
  295. // StartSpeek();
  296. //}
  297. if (str.Length > 0&& str != " ")
  298. {
  299. StartCoroutine(AudioIconSetting(0, false));
  300. // HttpsSendLog.Instance.SendLog("SendText", myt.text);
  301. UnityToJava.yiYanClass.CallStatic("SendText", str);
  302. backt.text = "";
  303. oldMyt = null;
  304. }
  305. //myt.text = "";
  306. }
  307. private IEnumerator OpenSpeek( float times)
  308. {
  309. yield return new WaitForSeconds(times);
  310. StartSpeek();
  311. }
  312. private IEnumerator PlayAnimator( string Animator_Name ,float times)
  313. {
  314. // animator.SetBool(Animator_Name, true);
  315. yield return new WaitForSeconds(times);
  316. //animator.SetBool(Animator_Name, false);
  317. }
  318. private IEnumerator AudioIconSetting(float times , bool state)
  319. {
  320. //if(state)
  321. //{
  322. // yield return new WaitForSeconds(0.5f);
  323. //}
  324. yield return new WaitForSeconds(times);
  325. ui.gameObject.SetActive(state);
  326. }
  327. }
  328. public class YiYanBack : AndroidJavaProxy
  329. {
  330. public static Queue<string> BackTextlist = new Queue<string>();
  331. public static Queue<string> BackFINALTextlist = new Queue<string>();
  332. public static Queue<string> MyTextlist = new Queue<string>();
  333. public static Queue<char> BackText = new Queue<char>();
  334. public YiYanBack() : base("com.xiaodu.dueros.magictool.demo.YiYanBack")
  335. {
  336. }
  337. public void onBackText(string msg)
  338. {
  339. Debug.Log("onBackText====>" + msg);
  340. BackTextlist.Enqueue(msg);
  341. // HttpsSendLog.Instance.SendLog("SendText", msg);
  342. for (int i = 0; i < msg.Length; i++)
  343. {
  344. BackText.Enqueue(msg[i]);
  345. }
  346. }
  347. public void onMyFINALText(string msg)
  348. {
  349. Debug.Log("onBackFINALText====>" + msg);
  350. BackFINALTextlist.Enqueue(msg);
  351. }
  352. public void onMyText(string msg)
  353. {
  354. Debug.Log("onMyText====>" + msg);
  355. MyTextlist.Enqueue(msg);
  356. }
  357. public void onWakeupSucceed(string msg)
  358. {
  359. Debug.Log("onMyText====>" + msg);
  360. MyTextlist.Enqueue(msg);
  361. }
  362. public void onWakeupFailed(string msg)
  363. {
  364. Debug.Log("onMyText====>" + msg);
  365. MyTextlist.Enqueue(msg);
  366. }
  367. }