我正在尝试创建一个 Windows 应用程序,我可以在其中获取我拥有的音频文件,并使用 Google Speech Recognition API 将其中的语音转录为文本文件。这是我所做的:
1)我去了这里https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev并成为会员。
2)我去了我的谷歌开发者控制台并成功生成了一个 API 密钥。
3)我在网上得到了一些代码并运行它:
private void btnGoogle_Click(object sender, EventArgs e)
{
string path = @"Z:\path\to\audio\file\good-morning-google.flac";
try
{
FileStream fileStream = File.OpenRead(path);
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=your-api-key-here");
_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();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("looks ok...");
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
Console.WriteLine(SR_Response.ReadToEnd());
Console.WriteLine(SR_Response.ReadToEnd());
Console.WriteLine("Done");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
上面的代码运行。它给了我以下输出:
looks ok...
{"result":[]}
因此,我知道我收到了HttpStatusCode.OK
响应,因为looks ok...
日志行已执行。
但是,结果完全是空的……这是为什么呢?难道我做错了什么?
编辑:这是我得到音频文件的地方:https ://github.com/gillesdemey/google-speech-v2