c#-使用语音命令启用和禁用语音识别
作者:互联网
我的问题分为两部分.
>有没有一种方法可以使用语音命令启用语音识别.
>是否可以禁用语音识别,但同时也可以使其处于侦听状态或启用该命令,以告知何时重新启用语音识别.
这就是我的想法:
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;
我知道这是行不通的,因为没有“ until”命令,那么我该怎么做呢?
解决方法:
我处理这个问题的方法是让它一直在听.我认为这也可能是“大人物”的做法,按照定义,如果您要使用语音命令开始收听,那么它已经必须在收听.
就我而言,我希望程序始终运行,因此我在构造函数中进行了设置.听起来这与您正在执行的操作类似:在此处为短语添加一个明确的名称会有所帮助(例如说“嘿Cortana”或“ 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");
}
}
标签:speech-recognition,windows,c,winforms 来源: https://codeday.me/bug/20191120/2040741.html