当前位置:   article > 正文

unity3d微软语音识别httppost失败。安全验证问题_invalid mime type "audio/wav; codecs=audio/pcm; sa

invalid mime type "audio/wav; codecs=audio/pcm; samplerate=16000": invalid t
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using UnityEngine;
  7. public class record : MonoBehaviour
  8. {
  9. AudioClip audioClip;
  10. AudioSource audioSource;
  11. public int recordTime=5;
  12. Use this for initialization
  13. void Start()
  14. {
  15. string[] md = Microphone.devices;
  16. int mdl = md.Length;
  17. if (mdl == 0)
  18. {
  19. Debug.Log("no microphone found");
  20. }
  21. }
  22. Update is called once per frame
  23. //void Update () {
  24. //}
  25. public void StartRecordAudio()
  26. {
  27. audioSource = GameObject.Find("Canvas/Audio Source").GetComponent<AudioSource>();
  28. Microphone.End(null);
  29. audioClip = Microphone.Start(null, false, recordTime, 16000);
  30. Debug.Log("开始录音.....");
  31. // if(Microphone.GetPosition())
  32. if (!Microphone.IsRecording(null))
  33. {
  34. Debug.Log("没有声音.....");
  35. return;
  36. }
  37. Microphone.GetPosition(null);
  38. }
  39. public void StopRecordAudio()
  40. {
  41. /***文件读取***
  42. FileInfo fi = new FileInfo("d:\\1.wav");
  43. FileStream fs = new FileStream("d:\\1.wav", FileMode.Open);
  44. byte[] buffer = new byte[fs.Length];
  45. fs.Read(buffer, 0, buffer.Length);
  46. fs.Close();
  47. ***/
  48. Microphone.End(null);
  49. // byte[] buffer = ConvertAudioClipToPCM16(audioClip);
  50. byte[] buffer = GetClipData();
  51. HttpWebRequest request = null;
  52. //request = (HttpWebRequest)HttpWebRequest.Create("https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=ZH-CN&format=detailed");
  53. request = (HttpWebRequest)HttpWebRequest.Create("http://vop.baidu.com/server_api?lan=zh&cuid=B8-81-98-41-3E-E9&token=24.91d00cdafeef1490ec706f7e2f2659e1.2592000.1524029061.282335-10681472");
  54. request.SendChunked = true;
  55. request.Accept = @"application/json;text/xml";
  56. request.Method = "POST";
  57. request.ProtocolVersion = HttpVersion.Version11;
  58. request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000";
  59. //request.Headers["Ocp-Apim-Subscription-Key"] = "e8cd273d62c347cb9f64d6b94b94435d";
  60. request.ContentLength = buffer.Length;
  61. // Send an audio file by 1024 byte chunks
  62. /*
  63. * Open a request stream and write 1024 byte chunks in the stream one at a time.
  64. */
  65. using (Stream requestStream = request.GetRequestStream())
  66. {
  67. requestStream.Write(buffer, 0, buffer.Length);
  68. }
  69. Debug.Log("Response:");
  70. string responseString;
  71. WebResponse response = request.GetResponse();
  72. Debug.Log(((HttpWebResponse)response).StatusCode);
  73. StreamReader sr = new StreamReader(response.GetResponseStream());
  74. responseString = sr.ReadToEnd();
  75. Debug.Log(responseString);
  76. }
  77. public static byte[] ConvertAudioClipToPCM16(AudioClip clip)
  78. {
  79. var samples = new float[clip.samples * clip.channels];
  80. clip.GetData(samples, 0);
  81. var samples_int16 = new short[samples.Length];
  82. for (var index = 0; index < samples.Length; index++)
  83. {
  84. var f = samples[index];
  85. samples_int16[index] = (short)(f * short.MaxValue);
  86. }
  87. var byteArray = new byte[samples_int16.Length * 2];
  88. Buffer.BlockCopy(samples_int16, 0, byteArray, 0, byteArray.Length);
  89. return byteArray;
  90. }
  91. /// <summary>
  92. /// 把录音转换为Byte[]
  93. /// </summary>
  94. /// <returns></returns>
  95. public byte[] GetClipData()
  96. {
  97. if (audioClip == null)
  98. {
  99. //Debug.LogError("录音数据为空");
  100. Debug.Log("录音数据为空");
  101. return null;
  102. }
  103. float[] samples = new float[audioClip.samples];
  104. audioClip.GetData(samples, 0);
  105. byte[] outData = new byte[samples.Length * 2];
  106. int rescaleFactor = 32767; //to convert float to Int16
  107. for (int i = 0; i < samples.Length; i++)
  108. {
  109. short temshort = (short)(samples[i] * rescaleFactor);
  110. byte[] temdata = System.BitConverter.GetBytes(temshort);
  111. outData[i * 2] = temdata[0];
  112. outData[i * 2 + 1] = temdata[1];
  113. }
  114. if (outData == null || outData.Length <= 0)
  115. {
  116. //Debug.LogError("录音数据为空");
  117. Debug.Log("录音数据为空");
  118. return null;
  119. }
  120. //return SubByte(outData, 0, audioLength * 8000 * 2);
  121. return outData;
  122. }
  123. public void PlayRecordAudio()
  124. {
  125. Microphone.End(null);
  126. audioSource.clip = audioClip;
  127. audioSource.Play();
  128. }
  129. public void EndPlayRecordAudio()
  130. {
  131. Microphone.End(null);
  132. audioSource.Stop();
  133. }
  134. }

1.微软报错百度可以

无论是读取文件的wav格式还是mcrophone录的音全部报错

是安全证书问题,mono这个项目总是这样,我以前写Xamarin安卓项目时候也有这个问题。

只能换www或者UnityWebRequest方式试一试。

已找到解决方法www可以

  1. TlsException: Invalid certificate received from server. Error code: 0xffffffff800b010a
  2. Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates)
  3. Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 ()
  4. Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process ()
  5. (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
  6. Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg)
  7. Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult)
  8. Rethrow as IOException: The authentication or decryption has failed.
  9. Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult)
  10. Rethrow as WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure
  11. System.Net.HttpWebRequest.EndGetRequestStream (IAsyncResult asyncResult)
  12. System.Net.HttpWebRequest.GetRequestStream ()
  13. record.StopRecordAudio () (at Assets/record.cs:76)
  14. UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
  15. UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
  16. UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
  17. UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
  18. UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
  19. UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
  20. UnityEngine.EventSystems.EventSystem:Update()

换成百度倒是会成功

2.在网页请求下

微软可以,百度也可以。

但微软可以识别效果更好一点。在高底噪情况下,百度识别不了,微软可以,可以说是不是一个等级的对手,百度的机器学习也许还需要加强。百度的语句识别也是非常不通顺。

有条件还是用微软还有Google,据说科大讯飞也不错,但没用过。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. namespace FunctionTest
  10. {
  11. public partial class voiceBiying : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. FileInfo fi = new FileInfo("d:\\1.wav");
  16. FileStream fs = new FileStream("d:\\1.wav", FileMode.Open);
  17. byte[] buffer = new byte[fs.Length];
  18. fs.Read(buffer, 0, buffer.Length);
  19. fs.Close();
  20. HttpWebRequest request = null;
  21. request = (HttpWebRequest)HttpWebRequest.Create("https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=ZH&format=detailed");
  22. //request = (HttpWebRequest)HttpWebRequest.Create("http://vop.baidu.com/server_api?lan=zh&cuid=B8-81-98-41-3E-E9&token=24.91d00cdafeef1490ec706f7e2f2659e1.2592000.1524029061.282335-10681472");
  23. request.SendChunked = true;
  24. request.Accept = @"application/json;text/xml";
  25. request.Method = "POST";
  26. request.ProtocolVersion = HttpVersion.Version11;
  27. request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000"
  28. //request.ContentType = @"audio/wav; rate=16000";
  29.             request.Headers["Ocp-Apim-Subscription-Key"] = "e8cd273d62c347cb9f64d6b94b94435d";
  30.             request.ContentLength = buffer.Length;
  31.             // Send an audio file by 1024 byte chunks
  32.             /*
  33.             * Open a request stream and write 1024 byte chunks in the stream one at a time.
  34.             */
  35.             using (Stream requestStream = request.GetRequestStream())
  36.             {
  37.                 requestStream.Write(buffer, 0, buffer.Length);
  38.             }
  39.             TextBox1.Text="Response:";
  40.             string responseString;
  41.             WebResponse response = request.GetResponse();
  42.             StreamReader sr = new StreamReader(response.GetResponseStream());
  43.             responseString = sr.ReadToEnd();
  44.             TextBox1.Text+=responseString;
  45.         }
  46.     }
  47. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/127048?site
推荐阅读
相关标签
  

闽ICP备14008679号