gpt4 book ai didi

c# - 使用语音命令启用和禁用语音识别

转载 作者:可可西里 更新时间:2023-11-01 09:24:04 27 4
gpt4 key购买 nike

我的问题分为两部分。

  1. 有没有办法使用语音命令启用语音识别。

  2. 有没有一种方法可以在禁用语音识别的同时,让它继续监听或启用命令以告知它何时再次启用语音识别。

这就是我的想法:

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”命令,所以我该怎么做呢?

最佳答案

我处理这个问题的方法是让它一直在听;我认为这可能也是“大人物”的做法,根据定义,如果您想使用语音命令开始收听,那么它已经必须在收听。

在我的例子中,我希望程序始终运行,所以我在构造函数中进行了设置。听起来这与您正在做的事情很相似:在此处将一个明确的名称添加到短语中会有所帮助(考虑说“Hey 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");
}
}

关于c# - 使用语音命令启用和禁用语音识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31380989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com