C#语音识别命令确认?

时间:2014-10-04 14:05:17

标签: c# command speech-recognition confirmation

所以我让应用程序认出我说的话。但是,当我命令它执行任务时,如何让应用程序确认请求?

截至目前,我有这段代码:

public partial class Form1 : Form
    {
        SpeechSynthesizer synth = new SpeechSynthesizer();
        SpeechRecognitionEngine sRecognize= new SpeechRecognitionEngine();

public Form1()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, EventArgs e)
        {
              Choices sList = new Choices();
              sList.Add(new String[] { "Exit"});

              Grammar gr = new Grammar(new GrammarBuilder(sList));

              sRecognize.RequestRecognizerUpdate();
              sRecognize.LoadGrammar(gr);
              sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
              sRecognize.SetInputToDefaultAudioDevice();
              sRecognize.RecognizeAsync(RecognizeMode.Multiple);
              sRecognize.SpeechRecognitionRejected += sRecognize_SpeechRecognitionRejected;

}

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
      if (e.Result.Text == "Exit")
      {
              Application.Exit();
      }
  }
}

所以我的问题就是一个例子:

  

我说,“退出”

申请通过以下方式确认:

  

您确定要退出吗?

根据我的回答,应用程序会响应。

作为确认,是请求取消。我需要做些什么改变?

1 个答案:

答案 0 :(得分:0)

这样的东西?

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
  if (e.Result.Text == "Exit")
  {
           Choices sList = new Choices();
          sList.Add(new String[] { "Yes"});

          Grammar gr = new Grammar(new GrammarBuilder(sList));

          sRecognize.RequestRecognizerUpdate();
          sRecognize.LoadGrammar(gr);
          sRecognize.SpeechRecognized += delegate(object sender,     SpeechRecognizedEventArgs e)   
                  {   
                       Application.Exit();
                  };
          sRecognize.SetInputToDefaultAudioDevice();
          sRecognize.RecognizeAsync(RecognizeMode.Multiple);
  }

}