我试图让我的windows窗体程序持续监听我的麦克风来检测语音,然后在gui上显示这些信息。
{
public class SpeechListener
{
GUI gui;
public SpeechListener(GUI gui) { this.gui = gui; }
public void StartListening() {
gui.setLabel("speech activated");
// Create an in-process speech recognizer for the en-US locale.
using (
SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
// Create and load a dictation grammar.
recognizer.LoadGrammar(new DictationGrammar());
// Add a handler for the speech recognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(Recognizer_SpeechRecognized);
// Configure input to the speech recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
}
// Handle the SpeechRecognized event.
public void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//this is where i want to get
gui.setLabel("Recognized text: " + e.Result.Text);
}
}
}
这个类被实例化,StartListening()被从一个窗体类(我的gui)中调用.我从来没有到达处理SpeechRecognized事件的方法。然而当我改变
recognizer.RecognizeAsync(RecognizeMode.Multiple)。
识别器.RecognizeAsync(RecognizeMode.Multiple);至
recognizer.Recognize()。
语音检测工作(但只有一次,而且它冻结了我的gui)。为什么异步方法不工作?我在一个控制台程序上使用了同样的代码,它工作得很完美。