Reputation: 97
There are 2 parts to my question.
Is there a way to enable speech recognition using voice commands.
Is there a way to disable speech recognition but also, at the same time, keep it listening or enabled for the command to tell it when to enable speech recognition again.
This is what I was thinking:
case "Stop Listening":
synthesizer.speakasync("Ok");
recEngine.RecognizeAsyncStop();
//Command or code here that wait's for the command "Start Listening"
recEngine.RecognizeAsyncStop "until" case "Start Listening"
break;
I know this won't work because there is no "until" command so how can I go about doing this?
Upvotes: 3
Views: 4558
Reputation: 5314
The way I handled this was to have it always listening; I think this is probably how the 'big guys' do it as well, as by definition, if you want to use a voice command to start listening, then it already has to be listening.
In my case, I wanted a program to always be running, so I set it up in the constructor. It sounds like this is similar to what you're doing: it helps here to have an explicit name to add to the phrase (think about saying "Hey Cortana" or "OK Google", it helps it to know you're trying to use it). Let's say you want your start phrase to be "Hey Larry, start listening to me". You would have a second SpeechRecognitionEngine
that's always there, but turned off if you're using your main engine, and turned back on when you want your main one to stop.
public partial class Form1 : Form
{
private Choices onOff = new Choices();
private Choices recChoices = new Choices();
private SpeechRecognitionEngine alwaysOnListener = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
private SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
public Form1()
{
InitializeComponent();
onOff.Add(new string[] {"Hey Larry start listening to me"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(onOff);
Grammar g = new Grammar(gb);
alwaysOnListener.LoadGrammar(g);
alwaysOnListener.SpeechRecognized += alwaysOnListener_SpeechRecognized;
recChoices.Add(new string[] {"Stop Listening"});
GrammarBuilder gb2 = new GrammarBuilder();
gb2.Append(recChoices);
Grammar recGrammar = new Grammar(gb2);
recEngine.LoadGrammar(recGrammar);
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text.Equals("Stop Listening"))
{
recEngine.RecognizeAsyncStop();
alwaysOnListener.RecognizeAsync(RecognizeMode.Multiple);
}
}
void alwaysOnListener_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text.Equals("Hey Larry start listening to me"))
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
alwaysOnListener.RecognizeAsyncStop();
}
}
private void button1_Click(object sender, EventArgs e)
{
alwaysOnListener.EmulateRecognize("Hey Larry start listening to me");
}
private void btnStop_Click(object sender, EventArgs e)
{
recEngine.EmulateRecognize("Stop Listening");
}
}
Upvotes: 3