在基於C#的應用程序中實現Google Speech API的代碼是什麼?我發現可以創建一個音頻文件並將其發送到http://slides.html5rocks.com/#speech-input並以文本形式接收。你能解釋一下如何做到這一點,或者如果你之前嘗試過這樣做,請提供代碼給我?已經在這裏卡住了一段時間使用Google Speech API
非常感謝。
到目前爲止的代碼:
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)
{
}
}
這並不谷歌返回任何值。
卡在哪裏?發佈您的嘗試,以及它不適合您的地方,SO不是免費代碼服務,我們不是爲您編寫代碼,而是幫助您獲得代碼的工作。 – Prix
@Prix完成!謝謝 – user3306938
歡迎來到Stack Overflow!請不要包含有關問題標題中使用的語言的信息,除非在沒有它的情況下沒有意義。標籤用於此目的。 –