So using Wit.ai I'm trying to use speech to text. I am using the Wit3D example from Github: https://github.com/afauch/wit3d/blob/master/Assets/UserScripts/Wit3D.cs
Recording of sound and saving to the .wav file works just fine. Sending the request to the server does not. The .wav file is valid as I get a response when manually making a request through Postman.
The request code looks like this:
string GetJSONText(string file)
{
// get the file w/ FileStream
FileStream filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader filereader = new BinaryReader(filestream);
byte[] BA_AudioFile = filereader.ReadBytes((Int32)filestream.Length);
filestream.Close();
filereader.Close();
//var bytes = File.ReadAllBytes(Path.Combine(Application.dataPath, "sample.wav"));
// create an HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.wit.ai/speech?v=20160901");
request.Method = "POST";
request.Headers["Authorization"] = "Bearer 3XFWDOBVS65V5A2VZWZFBB2PHOKDWGOH";
request.ContentType = "audio/wav";
//request.Timeout = 10000;
request.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
// Process the wit.ai response
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
print("Http went through ok");
StreamReader response_stream = new StreamReader(response.GetResponseStream());
return response_stream.ReadToEnd();
}
else
{
return "Error: " + response.StatusCode.ToString();
return "HTTP ERROR";
}
}
catch (Exception ex)
{
return "Error: " + ex.Message;
return "HTTP ERROR";
}
}
With or without putting a Timeout on the request I get the following Error message: "Error: The request timed-out"
Removing the line:
request.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length)
Will get me a response: Error: Error getting response stream (Write: The authentication or decryption has failed.) Which makes sense, because there is nothing to decrypt. My firewall doesn't seem to be the problem. Any ideas of why there is a time-out? Using different methods of getting a byte[] didn't fix it either.
EDIT: Putting the code in a normal Console application does work. So this seems to be a Unity issue.