Reputation: 886
i am trying to make an speech recognizer application .. Using C# . i have created the basic application it's working fine .
now my question is that .
how i can start and stop the speech recognizer using a Button Control .
i am using SpeechRecognitionEngine
Class
Here is my code .
private bool Status = false;
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Choices dic = new Choices(new String[] {
"word1",
"word2",
});
public Form1()
{
InitializeComponent();
Grammar gmr = new Grammar(new GrammarBuilder(dic));
gmr.Name = "myGMR";
// My Dic
sre.LoadGrammar(gmr);
sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
sre.SetInputToDefaultAudioDevice();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
private void button1_Click(object sender, EventArgs e)
{
if (Status)
{
button1.Text = "START";
Status = false;
stslable.Text = "Stopped";
}
else {
button1.Text = "STOP";
Status = true;
stslable.Text = "Started";
}
}
public void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs ev)
{
String theText = ev.Result.Text;
MessageBox.Show(theText);
}
Upvotes: 0
Views: 1321
Reputation: 13399
You can unsubscribe and dispose this object by setting this null, and when you need it, recreate and re-subscribe. Or you can unsubscribe only and it will not raise any event.
Upvotes: 1