GoogleSTTHubClient.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Net;
  2. using System.Net.Http.Headers;
  3. using System.Text;
  4. namespace TextToSpeechService;
  5. public class GoogleSTTHubClient
  6. {
  7. public HttpClient Client { get; private set; }
  8. public async Task Demo()
  9. {
  10. string inFile =AppDomain.CurrentDomain.BaseDirectory+ "/test/demo.wav";
  11. FileStream fs = new FileStream(inFile, FileMode.Open);
  12. byte[] voice = new byte[fs.Length];
  13. fs.Read(voice, 0, voice.Length);
  14. fs.Close();
  15. await Speech(voice);
  16. }
  17. public GoogleSTTHubClient(HttpClient httpClient)
  18. {
  19. httpClient.BaseAddress = new Uri("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN&maxresults=1");
  20. //httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
  21. //httpClient.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample");
  22. //httpClient.DefaultRequestHeaders.Add("content-type", "audio/x-flac; rate=16000");
  23. Client = httpClient;
  24. }
  25. /// <summary>
  26. /// api调不通
  27. /// 新版本的API: api:地址: https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=yourkey
  28. /// URL路径: 之前的V1变成V2
  29. /// 参数: 新增output:输出类型,我们一般用json 新增KEY:需要去google注册帐号,申请apikey 其他的参数都能够继续使用
  30. /// 开源代码:http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/
  31. /// </summary>
  32. /// <param name="voice"></param>
  33. /// <returns></returns>
  34. public async Task<string> Speech(byte[] voice)
  35. {
  36. HttpContent content = new StreamContent(new MemoryStream(voice));
  37. content.Headers.ContentType = new MediaTypeHeaderValue("audio/x-flac; rate=8000");
  38. var result= await Client.PostAsync("",content);
  39. await using Stream responseStream = await result.Content.ReadAsStreamAsync();
  40. using StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
  41. var text = await readStream.ReadToEndAsync();
  42. return text;
  43. }
  44. /// <summary>
  45. /// 调用google语音识别引擎
  46. /// </summary>
  47. /// <returns></returns>
  48. private string GoogleSTT(byte[] voice)
  49. {
  50. string result = string.Empty;
  51. try
  52. {
  53. //string inFile = "audio.wav";
  54. //FileStream fs = new FileStream(inFile, FileMode.Open);
  55. //byte[] voice = new byte[fs.Length];
  56. //fs.Read(voice, 0, voice.Length);
  57. //fs.Close();
  58. string url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN";
  59. Uri uri = new Uri(url);
  60. #pragma warning disable CS0618
  61. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  62. #pragma warning restore CS0618
  63. request.Method = "POST";
  64. request.ContentType = "audio/x-flac; rate=16000";
  65. request.ContentLength = voice.Length;
  66. using (Stream writeStream = request.GetRequestStream())
  67. {
  68. writeStream.Write(voice, 0, voice.Length);
  69. }
  70. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  71. {
  72. using (Stream responseStream = response.GetResponseStream())
  73. {
  74. using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
  75. {
  76. result = readStream.ReadToEnd();
  77. }
  78. }
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Console.WriteLine(ex.StackTrace);
  84. }
  85. return result;
  86. }
  87. }