What is the code for implementing the Google Speech API in my C# based application? I found out that it is possible to create an audio file and sent it to http://slides.html5rocks.com/#speech-input and receive it as text. Could you please explain how to do this or provide me with the code if you have attempted this before? Been stuck here for a while now
Much appreciated.
Code So far:
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
SpeechSynthesizer dummy = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
Choices searching = new Choices("Porsche");
GrammarBuilder searchService = new GrammarBuilder("Search");
searchService.Append(searching);
// Create a Grammar object from the GrammarBuilder and load it to the recognizer.
Grammar googleGrammar = new Grammar(searchService); ;
rec.RequestRecognizerUpdate();
rec.LoadGrammar(googleGrammar);
// Add a handler for the speech recognized event.
rec.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
// Configure the input to the speech recognizer.
rec.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
rec.RecognizeAsync(RecognizeMode.Multiple);
}
private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
try
{
FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
FS_Audiofile.Close();
BR_Audiofile.Close();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
_HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
textBox1.Text = SR_Response.ToString();
}
}
catch (Exception ex)
{
}
}
This does not return any value from Google.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…