ERNIEBotManager.cs 12 KB

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