Turn speech recognition on or off using speech commands
There are two parts to my question.
-
Is there a way to enable speech recognition using voice commands.
-
Is there a way to disable speech recognition but at the same time keep it listening or enabled for a command to tell it when to re-enable speech recognition.
Here's what I thought:
case "Stop Listening":
synthesizer.speakasync("Ok");
recEngine.RecognizeAsyncStop();
//Command or code here that wait for the command "Start Listening"
recEngine.RecognizeAsyncStop "until" case "Start Listening"
break;
I know it won't work because there is no "before" command, so how can I do that?
source to share
The way I dealt with it was that he always listened; I think this is probably the way the "big guys" do it, by definition, if you want to use a voice command to start listening, then it should already be listening.
In my case, I wanted the program to always work, so I installed it in the constructor. It looks like this is similar to what you are doing: it helps here to have an explicit name to add to the phrase (think about saying "Hey Cortana" or "OK ββGoogle", it helps to know that you are trying to use that) ... Let's say you want your starting line to be "Hey Larry, start listening to me." You will have a second one SpeechRecognitionEngine
, which is always there, but turns off if you use your main engine and turn on again when you want your main engine 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");
}
}
source to share