Skip to content

Instantly share code, notes, and snippets.

@tabbakka-developer
Created May 12, 2014 13:51
Show Gist options
  • Select an option

  • Save tabbakka-developer/75e639434b66e6c62e18 to your computer and use it in GitHub Desktop.

Select an option

Save tabbakka-developer/75e639434b66e6c62e18 to your computer and use it in GitHub Desktop.
Google Speech To Text C#
namespace GoogleSpeechToTextLib
{
public class GoogleVoice
{
public static string ACCESS_GOOGLE_SPEECH_KEY = "AIzaSyDC8nM1S0cLpXvRc8TXrDoey-tqQsoBGnM";
public static string TEST_NEW_PATH_2014_PART =
"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=";
public static string NOT_MY_KEY = "AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4";
public static String GoogleSpeechRequest(String flacName, int sampleRate)
{
PATH = TEST_NEW_PATH_2014_PART + ACCESS_GOOGLE_SPEECH_KEY;
HttpWebRequest request =(HttpWebRequest) HttpWebRequest.Create(PATH);
request.Method = "POST";
byte[] byteArray = File.ReadAllBytes(flacName);
sampleRate = 44100;
request.ContentType = "audio/x-flac; rate=" + sampleRate.ToString();
request.ContentLength = byteArray.Length;
Stream sendStream = request.GetRequestStream();
sendStream.Write(byteArray,0,byteArray.Length);
sendStream.Close();
string responseFromServer;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();
return responseFromServer;
}
}
}
@tabbakka-developer
Copy link
Author

I have some trouble with getting response. Google now return 2 lines, and i don't know how to get second line. Maybe someone can help me?

@welstrec
Copy link

You must parse the responseFromServer.
String [] jsons = responseFromServer.Split('\n');
String text="";
foreach (String j in jsons)
{
dynamic jsonObject = JsonConvert.DeserializeObject(j);
if (jsonObject == null || jsonObject.result.Count <= 0)
{
continue;
}
text = jsonObject.result[0].alternative[0].transcript;
}

@spike321
Copy link

i get ("result : [] )

I don't get the second line, how did you get the second line? (i used the same code)

@Pankajxicom
Copy link

I am getting this error " Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host." on following line " sendStream.Write(byteArray, 0, byteArray.Length);"
is this working at your end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment