123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using static TestFile;
- using LitJson;
- using UnityEngine.Android;
- public class ERNIEBotManager : MonoSingleton<ERNIEBotManager>
- {
- public bool isInit;
- public Text backt;
- public Text backfinalt;
- public Text myt;
- public RectTransform scrollRect;
- public GameObject ui;
- public Transform Irobot;
- // private Animator animator;
- public Transform player;
- private bool isCollect;
- private float times;
- private float times2;
- private float times3;
- private float backtTimes;
- private string oldMyt;
- private string oldback;
- private XunFeiYuYin xunfei;
- private float interval;
- private const string RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
- private bool hasRecordAudioPermission = false;
- private bool isWakeup = false;
- private Vector3 irobotPos;
- public bool isAudioPlay;
- private void Awake()
- {
- //animator = Irobot.GetComponent<Animator>();
-
- }
- private void OnEnable()
- {
- // transform.LookAt(API_GSXR_Slam.GSXR_Get_Head());
- // StartCoroutine(PlayAnimator("Waving", 1f));
-
- }
- void Start()
- {
- // 检查是否已经授权了麦克风权限
- hasRecordAudioPermission = Permission.HasUserAuthorizedPermission(RECORD_AUDIO_PERMISSION);
- if (!hasRecordAudioPermission)
- {
- // 请求麦克风权限
- Permission.RequestUserPermission(RECORD_AUDIO_PERMISSION);
- }
- isInit = true;
- isCollect = false;
- times = 0;
- oldMyt = null;
- oldback = null;
- backtTimes = 0;
- interval = 0.2f;
- irobotPos = new Vector3(0, -1, 6);
- HttpTool.Instance.Get("https://fat1.qulivr.com/mr-navigator/v1/", "", (string getInterval) => {
- Debug.Log(getInterval);
- if (getInterval == null)
- Debug.LogError(" HttpTool Get Interval Error!!! ");
- JsonData data = JsonMapper.ToObject(getInterval);
- Debug.Log(data["code"]);
- if ((int)data["code"] == 200)
- {
- Debug.Log(data["rate"].ToString());
- interval = float.Parse(data["rate"].ToString());
- float z = float.Parse(data["defaultZAxisValue"].ToString());
- float y = float.Parse(data["defaultHeight"].ToString());
- z = (string.IsNullOrEmpty(data["defaultZAxisValue"].ToString())) ? 1 : z;
- y = (string.IsNullOrEmpty(data["defaultHeight"].ToString())) ? 2 : y;
- irobotPos = new Vector3(0, y, z);
- }
- else
- Debug.LogError(" HttpTool Get Interval Error!!! ");
- });
- YiYanBack _setAndrodCallback = new YiYanBack();
- #if UNITY_ANDROID
- UnityToJava.initSpeek(_setAndrodCallback);
- isInit = true;
- isAudioPlay = false;
- xunfei = XunFeiYuYin.Init("708f4bb8", "MTQ0ZWZhMDE1ODA5NjVmMDYyODhmMTM3", "e2c01e5127d59a6ef92cef37e6255644", "");
- #endif
- // StartCoroutine(OpenSpeek(3f));
- }
- private IEnumerator Speek(float times)
- {
- yield return new WaitForSeconds(times);
- // UnityToJava.yiYanClass.CallStatic("SpeekOff", "HelloWord1234568999");
- }
-
- private void Update()
- {
- backtTimes += Time.deltaTime;
- if(backtTimes> interval && YiYanBack.BackText.Count>0)
- {
- char str = YiYanBack.BackText.Dequeue();
- backt.text += str;
- backtTimes = 0;
- times2 = 0;
- oldback = backt.text;
- // xunfei.startQueue(str.ToString());
- }
- if (YiYanBack.BackTextlist.Count > 0)
- {
- //backt.text += YiYanBack.BackTextlist.Dequeue();
- // YiYanBack.BackTextlist.Dequeue();
-
- xunfei.startQueue(YiYanBack.BackTextlist.Dequeue());
- //times2 = 0;
- //Debug.Log(backt.text);
- //oldback = backt.text;
- // StartCoroutine(PlayAnimator("Point_left", 1f));
- }
- if (YiYanBack.BackFINALTextlist.Count > 0)
- {
- backfinalt.text = YiYanBack.BackFINALTextlist.Dequeue();
- send(backfinalt.text);
-
- }
- if (YiYanBack.MyTextlist.Count > 0)
- {
- myt.text = YiYanBack.MyTextlist.Dequeue();
- oldMyt = myt.text;
- //HttpsSendLog.Instance.SendLog("MYT", myt.text);
- if (myt.text.Contains("唤醒成功") && myt.text.Contains("小度小度"))
- {
- myt.text = "";
- oldMyt = "";
-
- if (!Irobot.gameObject.activeSelf)
- {
- transform.position = player.position + player.forward * irobotPos.z;
- transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
- transform.LookAt(player);
- transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
- Irobot.gameObject.SetActive(true);
- isWakeup = true;
- }
- if (backt.text != "")
- {
- xunfei.stop();
- }
- StartCoroutine(AudioIconSetting(0, true));
- YiYanBack.BackText.Clear();
- YiYanBack.BackText = new Queue<char>();
- // backt.text = "";
- }
- else
- isWakeup = false;
- times2 = 0;
- times3 = 0;
- backt.text = "";
- HttpsSendLog.Instance.SendLog("MYT", oldMyt+" MY "+ myt.text);
- }
- if (oldback != null && oldback == backt.text && !isAudioPlay)
- {
- times2 += Time.deltaTime;
- if (times2 > 8f)
- {
- times2 = 0;
- isWakeup = false;
- Irobot.gameObject.SetActive(false);
- }
- }
- else
- times2 = 0;
-
- if (oldMyt != null && myt.text == oldMyt)
- {
- // HttpsSendLog.Instance.SendLog("MYT", " 计时开始 " + times);
- times += Time.deltaTime;
- if (times > 3f)
- {
- send(myt.text);
-
- oldMyt = null;
- times = 0;
- }
- }
- if (isWakeup)
- {
- times3 += Time.deltaTime;
- if (times3 > 10f)
- {
- times3 = 0;
- isWakeup = false;
- Irobot.gameObject.SetActive(false);
- }
- }
- #region old
- /*
- //if (oldback != null && oldback == backt.text)
- //{
- // times2 += Time.deltaTime;
- // if (times2 > 3f)
- // {
- // oldback = null;
- // times2 = 0;
- // StartSpeek();
- // // StartCoroutine(OpenSpeek(1f));
- // }
- //}
- //else
- // times2 = 0;
- //if (oldMyt!=null && myt.text == oldMyt)
- //{
- // if(!isWakeup&&myt.text.Contains("小度小度"))
- // {
- // isWakeup = true;
- // transform.position = player.position + player.forward * irobotPos.z;
- // transform.position = new Vector3(transform.position.x, irobotPos.y, transform.position.z);
- // transform.LookAt(player);
- // transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
- // Irobot.gameObject.SetActive(true);
- // myt.text = "";
- // backfinalt.text = "";
- // backt.text = "";
- // // StartCoroutine(OpenSpeek(0.3f));
- // StartSpeek();
- // }
- // times += Time.deltaTime;
- // if(times>1f)
- // StartCoroutine(AudioIconSetting(0, false));
- // if (times > 3f)
- // {
- // oldMyt = null;
- // times = 0;
- // send(myt.text);
- // }
- //}else
- //{
- // times = 0;
- //}
- */
- #endregion
- }
- private void OnApplicationPause(bool pause)
- {
- if (isInit)
- {
- if (pause)
- {
- UnityToJava.yiYanClass.CallStatic("onPauseJava");
- }
- else
- {
- UnityToJava.yiYanClass.CallStatic("onResumeJava");
- }
- }
- }
- private void OnDestroy()
- {
- UnityToJava.yiYanClass.CallStatic("onDestroyJava");
- }
- public void StopSpeek()
- {
- UnityToJava.yiYanClass.CallStatic("StopSpeek");
- Debug.Log("StopSpeek====");
- }
- public void StartSpeekOnClick()
- {
- StartSpeek();
- YiYanBack.BackText.Clear();
- YiYanBack.BackText = new Queue<char>();
- myt.text = "";
- backt.text = "";
- }
- public void StartSpeek()
- {
-
-
- //UnityToJava.yiYanClass.CallStatic("StartSpeek");
- //Debug.Log("StartSpeek====");
- //StartCoroutine(AudioIconSetting(0.7f, true));
- }
- public void send(string str)
- {
- //StartCoroutine(AudioIconSetting(0, false));
- //if (isWakeup)
- //{
- // if (myt.text.Contains("小度拜拜"))
- // {
- // isWakeup = false;
- // Irobot.gameObject.SetActive(false);
- // StartSpeek();
- // }
- // else
- // {
- // UnityToJava.yiYanClass.CallStatic("SendText", str);
- // Debug.Log("Send " + str);
- // }
- //}
- //else
- //{
- // StartSpeek();
- //}
- StartCoroutine(AudioIconSetting(0, false));
- HttpsSendLog.Instance.SendLog("SendText", myt.text);
- UnityToJava.yiYanClass.CallStatic("SendText", str);
- backt.text = "";
- oldMyt = null;
- //myt.text = "";
- }
- private IEnumerator OpenSpeek( float times)
- {
- yield return new WaitForSeconds(times);
- StartSpeek();
- }
- private IEnumerator PlayAnimator( string Animator_Name ,float times)
- {
- // animator.SetBool(Animator_Name, true);
- yield return new WaitForSeconds(times);
- //animator.SetBool(Animator_Name, false);
- }
- private IEnumerator AudioIconSetting(float times , bool state)
- {
- //if(state)
- //{
- // yield return new WaitForSeconds(0.5f);
-
- //}
- yield return new WaitForSeconds(times);
- ui.gameObject.SetActive(state);
- }
-
- }
- public class YiYanBack : AndroidJavaProxy
- {
- public static Queue<string> BackTextlist = new Queue<string>();
- public static Queue<string> BackFINALTextlist = new Queue<string>();
- public static Queue<string> MyTextlist = new Queue<string>();
- public static Queue<char> BackText = new Queue<char>();
- public YiYanBack() : base("com.xiaodu.dueros.magictool.demo.YiYanBack")
- {
- }
- public void onBackText(string msg)
- {
- Debug.Log("onBackText====>" + msg);
- BackTextlist.Enqueue(msg);
-
- HttpsSendLog.Instance.SendLog("F", msg);
- for (int i = 0; i < msg.Length; i++)
- {
- BackText.Enqueue(msg[i]);
- }
- }
- public void onMyFINALText(string msg)
- {
- Debug.Log("onBackFINALText====>" + msg);
- BackFINALTextlist.Enqueue(msg);
- }
- public void onMyText(string msg)
- {
- Debug.Log("onMyText====>" + msg);
- MyTextlist.Enqueue(msg);
- }
- public void onWakeupSucceed(string msg)
- {
- Debug.Log("onMyText====>" + msg);
- MyTextlist.Enqueue(msg);
- }
- public void onWakeupFailed(string msg)
- {
- Debug.Log("onMyText====>" + msg);
- MyTextlist.Enqueue(msg);
- }
- }
|