2014-02-16 69 views
0

在基於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) 
     { 

     } 
    } 

這並不谷歌返回任何值。

+1

卡在哪裏?發佈您的嘗試,以及它不適合您的地方,SO不是免費代碼服務,我們不是爲您編寫代碼,而是幫助您獲得代碼的工作。 – Prix

+0

@Prix完成!謝謝 – user3306938

+1

歡迎來到Stack Overflow!請不要包含有關問題標題中使用的語言的信息,除非在沒有它的情況下沒有意義。標籤用於此目的。 –

回答

2

只要發送的文件不會太長... 5秒以下,下面的捲曲工作。

捲曲-X POST -H 「內容類型:音頻/ X-後手;率= 16000」 \ -T seg_1.flac「https://www.google.com/speech-api/v1/recognize \ xjerr = 1個&客戶= speech2text &的maxResults = 1 &? LANG = EN-US &鍵= ... 48593"

{ 「地位」:0, 「ID」: 「」, 「假設」:[{ 「話語」: 「現在這是 最喜歡的消遣」 ,「confidence」:0.95148802}]}

所以,編碼到speechX或flac

包括與記錄您的採樣率PARM

包括你的鑰匙

保持文件的持續時間短(你將不得不之前API訪問分割文件)

+0

https://github.com/gillesdemey/google-speech-v2查看更新 –

1

的FS_Audiofile FILESTREAM你發送給Google的信息是空的,這就是爲什麼你沒有收到任何迴應。

你缺少這樣調用:

recognizedAudio.WriteToAudioStream(FS_Audiofile); 
相關問題