以下简单代码尝试将wave文件发布到Google Speech2Text服务,但总是失败并显示“Gateway Timeout(504)”或一般异常“Operation timed out”.有人可以帮忙吗?
public void ProcessWaveFile(string path)
{
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(
"https://www.google.com/"+
"speech-api/v1/recognize?"+
"xjerr=1&client=speech2text&lang=en-US&maxresults=10");
ServicePointManager.ServerCertificateValidationCallback +=
delegate { return true; };
request.Timeout = 60000;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "audio/wav";
request.UserAgent = "speech2text";
FileInfo fInfo = new FileInfo(path);
long numBytes = fInfo.Length;
byte[] data;
using (FileStream fStream = new FileStream(
path,
FileMode.Open,
FileAccess.Read))
{
data = new byte[fStream.Length];
fStream.Read(data, 0, (int)fStream.Length);
fStream.Close();
}
using (Stream wrStream = request.GetRequestStream())
wrStream.Write(data, 0, data.Length);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var resp = response.GetResponseStream();
if (resp != null)
{
StreamReader sr = new StreamReader(resp);
MessageBox.Show(sr.ReadToEnd());
resp.Close();
resp.Dispose();
}
}
catch (System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}
非常感谢.
舒贾特
最佳答案 您的代码适用于我,但有以下更改:
Request.ContentType = "audio/x-flac; rate=8000";
并且您提供的文件需要采用FLAC格式.
我用Windows Sound Recorder录制了一个小样本,它生成了一个WMA文件.然后我使用VLC Player将WMA文件转换为FLAC(使用转换选项,确保以RAW格式输出,一个通道和8000 kbps)
一个好的non c# reference以及评论中的一些扩展api文档