Ejemplo n.º 1
0
        // 把语音转换为文字
        private IEnumerator GetAudioString(string speech, int len, delegateTranslateComplete callBack)
        {
            //sToken = "24.90902d79c6ee916b366106a4b8eea0e0.2592000.1520950489.282335-10805541";
            JSONObject jsSend = JSONObject.Create();

            jsSend.AddField("format", "amr");
            jsSend.AddField("rate", 16000);
            jsSend.AddField("channel", "1");
            jsSend.AddField("cuid", GameDataMgr.PLAYER_DATA.Uuid);
            jsSend.AddField("token", sToken);
            jsSend.AddField("speech", speech);
            jsSend.AddField("len", len);
            string jsData = jsSend.ToString();

            byte[] post_byte = System.Text.Encoding.UTF8.GetBytes(jsData);

            var www = new UnityWebRequest(sound_to_text_url, "POST");

            www.uploadHandler             = (UploadHandler) new UploadHandlerRaw(post_byte);
            www.downloadHandler           = (DownloadHandler) new DownloadHandlerBuffer();
            www.uploadHandler.contentType = "application/json; charset=utf-8";
            www.SetRequestHeader("Content-Type", "application/json; charset=utf-8");

            yield return(www.Send());

            if (www.isError)
            {
                www.Dispose();
                yield return(new WaitForSeconds(0.5f));

                www = new UnityWebRequest(sound_to_text_url, "POST");
                www.uploadHandler             = (UploadHandler) new UploadHandlerRaw(post_byte);
                www.downloadHandler           = (DownloadHandler) new DownloadHandlerBuffer();
                www.uploadHandler.contentType = "application/json";
                www.SetRequestHeader("Content-Type", "application/json");
                yield return(www.Send());
            }
            if (www.isError)
            {
                www.Dispose();
                yield return(new WaitForSeconds(0.5f));

                www = new UnityWebRequest(sound_to_text_url, "POST");
                www.uploadHandler             = (UploadHandler) new UploadHandlerRaw(post_byte);
                www.downloadHandler           = (DownloadHandler) new DownloadHandlerBuffer();
                www.uploadHandler.contentType = "application/json";
                www.SetRequestHeader("Content-Type", "application/json");
                yield return(www.Send());
            }
            if (www.isDone)
            {
                if (!www.isError)
                {
                    AcceptanceIdentification acceptData = new AcceptanceIdentification();
                    JsonUtility.FromJsonOverwrite(www.downloadHandler.text, acceptData);
                    if (acceptData != null)
                    {
                        string rlt = acceptData.result[0];
                        if (rlt.Substring(rlt.Length - 1, 1) == ",")
                        {
                            rlt = rlt.Substring(0, rlt.Length - 1);
                        }
                        Debug.Log(rlt);
                        rlt = GameText.Instance.StrFilter(rlt);
                        //UtilTools.MessageDialog(rlt);
                        if (callBack != null)
                        {
                            callBack(0, rlt);
                        }
                    }
                    else
                    {
                        if (callBack != null)
                        {
                            callBack(1, "");
                        }
                    }
                }
                else
                {
                    Debug.LogError(www.error);
                    if (callBack != null)
                    {
                        callBack(2, "");
                    }
                }
            }
        }
Ejemplo n.º 2
0
 //声音转文字
 public void SoundToText(string sSoundData, int len, delegateTranslateComplete callBack)
 {
     StartCoroutine(GetAudioString(sSoundData, len, callBack));
 }