我的问题有两个部分。
这就是我的想法:
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;
我知道这行不通,因为没有“直到”命令,所以我该怎么做呢?
最佳答案
我处理这个问题的方式是让它一直在听;我认为这可能也是“大人物”的做法,根据定义,如果您想使用语音命令开始聆听,那么它必须已经在聆听。
就我而言,我希望程序始终运行,因此我在构造函数中设置了它。听起来这与您正在做的事情类似:在这里有一个明确的名称来添加到短语中会有所帮助(考虑说“你好小娜”或“OK Google”,这有助于它知道您正在尝试用它)。假设您希望您的开始短语是“嘿拉里,开始听我说”。您将有第二个 SpeechRecognitionEngine
始终存在,但如果您正在使用主引擎,则将其关闭,并在您希望主引擎停止时重新打开。
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");
}
}
关于c# - 使用语音命令启用和禁用语音识别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31380989/