Where am I wrong?

Yairk_kaufmann 6 Reputation points
2021-03-12T18:28:30.22+00:00

Where am I wrong?
it's not do anything when I speak

this is my code:

using System;
     using System.Collections.Generic;
     using System.ComponentModel;
     using System.Data;
     using System.Drawing;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using System.Windows.Forms;
     using System.Speech.Recognition;
     using System.Speech.Synthesis;
     using System.IO;

     namespace WindowsFormsApp12
     {
         public partial class Form1 : Form
         {
             SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
             SpeechSynthesizer Sarah = new SpeechSynthesizer();
             SpeechRecognitionEngine startListening = new SpeechRecognitionEngine();
             Random rnd = new Random();
             int RectTimeOut = 0;
             DateTime TimeNow = DateTime.Now;
             public Form1()
             {
                 InitializeComponent();


             }

             private void Form1_Load(object sender, EventArgs e)
             {
                 _recognizer.SetInputToDefaultAudioDevice();
                 _recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"DefaultCommends.txt")))));
                 _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(DefaultSpeechRecognized);
                 _recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(_recognizer_speechRecognized);
                 _recognizer.RecognizeAsync(RecognizeMode.Multiple);
                 startListening.SetInputToDefaultAudioDevice();
                 startListening.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"DefaultCommends.txt")))));
                 startListening.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(startlistening_SpeechReconnized);
             }

             private void DefaultSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
             {
                 int RunNum;
                 string Speach = e.Result.Text;
                 if(Speach == "Hello")
                 {
                     Sarah.SpeakAsync("Hello, i am here");
                 }

                 if(Speach == "How are you")
                 {
                     Sarah.SpeakAsync("im working normaly");
                 }

                 if (Speach == "What the time now")
                 {
                     Sarah.SpeakAsync(DateTime.Now.ToString("h mm tt"));
                 }

                 if(Speach == "Stop talking")
                 {
                     Sarah.SpeakAsyncCancelAll();
                     RunNum = rnd.Next(1, 2);

                     if(RunNum == 1)
                     {
                         Sarah.SpeakAsync("yes sir");
                     }

                     if(RunNum == 2)
                     {
                         Sarah.SpeakAsync("i am sorry i am be quiet");
                     }
                 }

                 if(Speach == "Stop listening")
                 {
                     Sarah.SpeakAsync("if you need me just ask");
                     _recognizer.RecognizeAsyncCancel();
                     startListening.RecognizeAsync(RecognizeMode.Multiple);

                 }

                 if (Speach == "Show Commends")
                 {
                     string[] Commends = (File.ReadAllLines(@"DefaultCommends.txt"));
                     LstCOmmends.Items.Clear();
                     LstCOmmends.SelectionMode = SelectionMode.None;
                     LstCOmmends.Visible = true;

                     foreach(string commend in Commends)
                     {
                         LstCOmmends.Items.Add(commend);
                     }
                 }

                 if(Speach == "Hide commends")
                 {
                     LstCOmmends.Visible = false;
                 }


             }

             private void _recognizer_speechRecognized(object sender, SpeechDetectedEventArgs e)
             {
                 RectTimeOut = 0;
             }

             private void startlistening_SpeechReconnized(object sender, SpeechRecognizedEventArgs e)
             {
                 string Speech = e.Result.Text;

                 if(Speech == "Wake Up")
                 {
                     startListening.RecognizeAsyncCancel();
                     Sarah.SpeakAsync("yes, im here");
                     _recognizer.RecognizeAsync(RecognizeMode.Multiple);
                 }
             }








             private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
             {

             }

             private void TmrSpeaking_Tick(object sender, EventArgs e)
             {
                 if(RectTimeOut == 10)
                 {
                     _recognizer.RecognizeAsyncCancel();
                 }
                 else if( RectTimeOut == 11)
                 {
                     TmrSpeaking.Stop();
                     startListening.RecognizeAsync(RecognizeMode.Multiple);
                     RectTimeOut = 0;
                 }
             }

             private void button1_Click(object sender, EventArgs e)
             {
                 Sarah.SpeakAsync("yes, im here");
             }


         }
     }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,298 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-03-15T06:28:22.9+00:00

    The strange thing is that I didn't got anything wrong.

    I commented out the TmrSpeaking_Tick method because it seemed useless in this code, and then modified the file path, ran the program, and everything worked fine.

    It can correctly recognize my words and respond according to the preset answers.

    As shown below, I say "Show Commends" and then "Hide Commends".

    77675-1.gif

    Are those commands added correctly in your DefaultCommends.txt file?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments