使用Google Speech API

在基于C#的应用程序中实现Google Speech API的代码是什么? 我发现可以创建一个音频文件并将其发送到http://slides.html5rocks.com/#speech-input并以文本forms接收。 如果你以前尝试过,可以解释一下如何做到这一点或者提供代码吗? 被困在这里一段时间了

非常感激。

代码到目前为止:

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(_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) { } } 

这不会从Google返回任何价值。

只要发送的文件不会太长,以下工作在curl中…不到5秒。

curl -X POST -H“Content-Type:audio / x-flac; rate = 16000”\ -T seg_1.flac“ https://www.google.com/speech-api/v1/recognize ?\ xjerr = 1&client = speech2text&的maxResults = 1&LANG = EN-US&关键= … 48593″

{“status”:0,“id”:“”,“假设”:[{“话语”:“现在是最喜欢的消遣”,“置信度”:0.95148802}]}

因此,编码为speechX或flac

在记录中包含一个带有采样率的parm

包括你的钥匙

保持文件持续时间短(您必须在API访问之前拆分文件)

您发送给Google的FS_Audiofile文件流是空的,这就是您没有收到任何回复的原因。

你错过了这个电话:

 recognizedAudio.WriteToAudioStream(FS_Audiofile);