Search code examples
c#commandspeech-recognitionconfirmation

C# Speech Recognition Command Confirmation?


So I've made the application recognize what I say. But how do I make the application confirm a request when I command it to carry out a task?

As of now I have this code:

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();
      }
  }
}

So my question as an example:

I say, "Exit"

Application confirms by:

Are you sure you want to exit?

And depending on my answer, the application responds.

Yes being a confirmation and No being a request cancellation. What changes do I have to make?


Solution

  • Something like this?

    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);
      }
    

    }