2016-07-05 72 views
2

我試圖創建一個語音識別程序,該程序需要在鎖定的Windows計算機上運行,​​作爲家庭自動化項目的一部分。但似乎SpeechRecognitionEngine在計算機被鎖定時停止識別(並在計算機解鎖時繼續)。SpeechRecognitionEngine停止識別計算機鎖定

我目前的測試程序是這樣的:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.Speech.Recognition; 
using System.Globalization; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     SpeechRecognitionEngine sre; 

     public Form1() 
     { 
      InitializeComponent(); 
      CultureInfo ci = new CultureInfo("en-us"); 
      sre = new SpeechRecognitionEngine(ci); 
      sre.SetInputToDefaultAudioDevice(); 
      GrammarBuilder gb = new GrammarBuilder("Hello"); 
      sre.LoadGrammarAsync(new Grammar(gb)); 
      sre.SpeechRecognized += sre_SpeechRecognized; 
      sre.RecognizeAsync(RecognizeMode.Multiple); 
     } 

     void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
      listBox1.Items.Add(DateTime.Now.ToString() + " " + e.Result.Text); 
     } 
    } 
} 

我想知道如果有可能的SpeechRecognitionEngine(也許使用SetInputToAudioStreamSetInputToWaveStream方法)的輸入切換到麥克風的現場音頻流輸入,並以這種方式解決問題。因爲計算機似乎沒有關閉麥克風(使用SoundRecorder進行嘗試)。

不幸的是,我一直無法找到一種方法來獲得麥克風輸入的實時流。

+0

我已經用錄音機錄製當計算機沒有問題鎖定嘗試。而且我還製作了一個使用NAudio將麥克風連接到揚聲器的小程序。這也適用於電腦鎖定。不幸的是,我找不到一種方法讓NAudio提供我可以用於SpeechRecognitionEngine的流。因此,當電腦鎖定時,麥克風似乎可用。而關於使用服務,那麼系統服務似乎無法使用麥克風和揚聲器。 –

回答

2

我找到了一個使用NAudio(http://naudio.codeplex.com/)和這個StackOverflow答案中的SpeechStreamer類的解決方法(https://stackoverflow.com/a/11813276/2950065)。

更新的測試程序,即繼續當計算機被鎖定認識到,看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.Speech.Recognition; 
using System.Globalization; 
using NAudio.Wave; 
using System.IO; 
using System.IO.Pipes; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     SpeechRecognitionEngine sre; 
     WaveIn wi; 
     SpeechStreamer ss; 

     public Form1() 
     { 
      InitializeComponent(); 

      WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback(); 
      wi = new WaveIn(callbackInfo); 
      ss = new SpeechStreamer(100000); 
      wi.DataAvailable += wi_DataAvailable; 
      wi.StartRecording(); 

      CultureInfo ci = new CultureInfo("en-us"); 
      sre = new SpeechRecognitionEngine(ci); 
      // The default format for WaveIn is 8000 samples/sec, 16 bit, 1 channel 
      Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo safi = new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(8000, Microsoft.Speech.AudioFormat.AudioBitsPerSample.Sixteen, Microsoft.Speech.AudioFormat.AudioChannel.Mono); 
      sre.SetInputToAudioStream(ss, safi); 
      GrammarBuilder gb = new GrammarBuilder("Hello"); 
      sre.LoadGrammarAsync(new Grammar(gb)); 
      sre.SpeechRecognized += sre_SpeechRecognized; 
      sre.RecognizeAsync(RecognizeMode.Multiple); 
     } 

     void wi_DataAvailable(object sender, WaveInEventArgs e) 
     { 
      ss.Write(e.Buffer, 0, e.BytesRecorded); 
     } 

     void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
      listBox1.Items.Add(DateTime.Now.ToString() + " " + e.Result.Text); 
     } 
    } 
}