public void OnEnter() { string txt = inputField.text; if (string.IsNullOrEmpty(txt)) { return; } showText.text = "我" + ":" + txt + "\n"; CallTuring(txt, delegate(string v_result) { RobotshowText.text = "我的机器人:" + v_result + "\n"; StartCoroutine(ctrl.GetAudioClip(v_result, (x) => { if (x != null) { AudioSource.PlayClipAtPoint(x, transform.position); } else { Debug.Log("clip:" + x); } }, parmas)); }); }
private void OnGUI() { parmas.text = GUILayout.TextField(parmas.text); if (GUILayout.Button("Play")) { StartCoroutine(ctrl.GetAudioClip(parmas.text, (x) => { if (x != null) { AudioSource.PlayClipAtPoint(x, transform.position); } else { Debug.Log("clip:" + x); } }, parmas)); } }
/// <summary> /// 发起语音识别请求 /// </summary> /// <returns></returns> IEnumerator _StartBaiduYuYin() { if (string.IsNullOrEmpty(accessToken)) { yield return(_GetAccessToken()); } asrResult = string.Empty; //处理当前录音数据为PCM16 float[] samples = new float[recordFrequency * trueLength * saveAudioClip.channels]; saveAudioClip.GetData(samples, 0); var samplesShort = new short[samples.Length]; for (var index = 0; index < samples.Length; index++) { samplesShort[index] = (short)(samples[index] * short.MaxValue); } byte[] datas = new byte[samplesShort.Length * 2]; Buffer.BlockCopy(samplesShort, 0, datas, 0, datas.Length); string url = string.Format("{0}?cuid={1}&token={2}", "https://vop.baidu.com/server_api", SystemInfo.deviceUniqueIdentifier, accessToken); WWWForm wwwForm = new WWWForm(); wwwForm.AddBinaryData("audio", datas); UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, wwwForm); unityWebRequest.SetRequestHeader("Content-Type", "audio/pcm;rate=" + recordFrequency); yield return(unityWebRequest.SendWebRequest()); if (string.IsNullOrEmpty(unityWebRequest.error)) { asrResult = unityWebRequest.downloadHandler.text; if (Regex.IsMatch(asrResult, @"err_msg.:.success")) { Match match = Regex.Match(asrResult, "result.:..(.*?)..]"); if (match.Success) { asrResult = match.Groups[1].ToString(); } } else { asrResult = "识别结果为空"; flag = -1; } textResult.text = asrResult; if (flag == 1) { showText.text = "我" + ":" + textResult.text + "\n"; // tulingchat = new TuLing(); Obj1.GetComponent <TuLing>().CallTuring(textResult.text, delegate(string v_result) // TuLing.CallTuring(textResult.text, delegate (string v_result) { Debug.Log("Calltuling"); RobotshowText.text = "我的机器人:" + v_result + "\n"; StartCoroutine(ctrl.GetAudioClip(v_result, (x) => { if (x != null) { AudioSource.PlayClipAtPoint(x, transform.position); } else { Debug.Log("clip:" + x); } }, parmas)); }); } else { showText.text = textResult.text; } } }