简体   繁体   English

Api Speech v2 Google结果为空

[英]Api speech v2 google result empty

I use google api speech. 我使用Google API语音。 But my function always return two results: {"result": []} and "". 但是我的函数总是返回两个结果:{“ result”:[]}和“”。 I am looking for a solution, but I can not find. 我正在寻找解决方案,但找不到。 My audio file is there http://custa.web44.net/10002_en.flac 我的音频文件在那里http://custa.web44.net/10002_zh.flac

 FileStream fileStream = System.IO.File.OpenRead(Server.MapPath("/10002_en.flac"));
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.SetLength(fileStream.Length);
                fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
                byte[] BA_AudioFile = memoryStream.GetBuffer();
                HttpWebRequest _HWR_SpeechToText = null;
                _HWR_SpeechToText = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=my-key");
                _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
                _HWR_SpeechToText.Method = "POST";
                _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
                _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
                Stream stream = _HWR_SpeechToText.GetRequestStream();
                stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
                stream.Close();
                HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();

                StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
                string responseFromServer = (SR_Response.ReadToEnd());

                String[] jsons = responseFromServer.Split('\n');

Your link didn't work but it's usually the bit rate and stereo that is the problem. 您的链接无效,但通常是比特率和立体声问题。 Use http://www.audacityteam.org/ and drop it to 16-bit PCM and convert to mono. 使用http://www.audacityteam.org/并将其拖放到16位PCM并转换为单声道。

Here is some simpler code: 这是一些简单的代码:

string api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string path = @"C:\temp\good-morning-google.flac";

byte[] bytes = System.IO.File.ReadAllBytes(path);

WebClient client = new WebClient();
client.Headers.Add("Content-Type", "audio/x-flac; rate=44100");
byte[] result = client.UploadData(string.Format(
            "https://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-us&key={0}", api_key), "POST", bytes);

string s = client.Encoding.GetString(result);

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM