C# Google Speech api v2 返回空字符串
C# Google Speech api v2 returning empty string
大约一周前,我的 google 语音 v2 API 工作得非常好,它返回的结果没有任何问题,但是今天使用相同的 .flac 文件测试它时不断返回“{\ "result\":[]}" 无论我尝试什么。想知道是否
A)其他人有这个问题或
B) 任何人都能解决我的问题
我的代码在下面谢谢!
public static String gvoice ()
{
//set the input file name
FileStream fileStream = File.OpenRead(@"test1.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;
//this points to the google speech API (key goes at end after &key=)
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create(
"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=" + key);
_HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
_HWR_SpeechToText.Method = "POST";
//sets kMhz and file type (flac)
_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)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
string result = SR_Response.ReadToEnd();
return result;
}
else
{
return "error";
}
}
A) Anyone else is having this problem
当然,Google 对 API 施加了使用限制,这使得它不如您想象的那么实用。
or B) Anyone has a solution to my problem my code is below thanks!
用其他API的,有很多
声音文件的要求已更改。它应该是 MONO 16000 速率的 Flac 文件
大约一周前,我的 google 语音 v2 API 工作得非常好,它返回的结果没有任何问题,但是今天使用相同的 .flac 文件测试它时不断返回“{\ "result\":[]}" 无论我尝试什么。想知道是否 A)其他人有这个问题或 B) 任何人都能解决我的问题 我的代码在下面谢谢!
public static String gvoice ()
{
//set the input file name
FileStream fileStream = File.OpenRead(@"test1.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;
//this points to the google speech API (key goes at end after &key=)
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create(
"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=" + key);
_HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
_HWR_SpeechToText.Method = "POST";
//sets kMhz and file type (flac)
_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)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
string result = SR_Response.ReadToEnd();
return result;
}
else
{
return "error";
}
}
A) Anyone else is having this problem
当然,Google 对 API 施加了使用限制,这使得它不如您想象的那么实用。
or B) Anyone has a solution to my problem my code is below thanks!
用其他API的,有很多
声音文件的要求已更改。它应该是 MONO 16000 速率的 Flac 文件