问题描述
所以我的程序遇到了一些问题,我不明白为什么。在下面的命令正文中,有一堆 if 语句,其中一些会起作用,而另一些则不起作用。如果它有帮助,我正在使用视频来帮助我,因为我是 C# 的新手。您可以找到上述视频 here。我遇到的主要问题是只有“你好约翰”、“你是谁”和“现在几点”命令才有效。所有其他命令都失败。我已经确保所有的库都是导入的,所以我知道不是那样的。
namespace Voice_Assistant_Project
{ 公共部分类 Form1 :表单 { SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine(); SpeechSynthesizer John = new SpeechSynthesizer(); SpeechRecognitionEngine StartListening = new SpeechRecognitionEngine(); 随机 rnd = 新随机(); double RecTimeOut = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammarasync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"DefaultCommands.txt")))));
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Default_SpeechRecognized);
_recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(_recognizer_SpeechRecognized);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
StartListening.SetInputToDefaultAudioDevice();
StartListening.LoadGrammarasync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"DefaultCommands.txt")))));
StartListening.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(StartListening_SpeechRecognized);
}
private void Default_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
{
int ranNum;
string speech = e.Result.Text;
if (speech == "hello john")
{
John.SpeakAsync("Greetings master");
}
if (speech == "who are you")
{
John.SpeakAsync("I am John,your personal voice assistant,and creation");
}
if (speech == "Commands")
{
John.SpeakAsync("Here are the commands that you can give me");
string[] commands = (File.ReadAllLines(@"DefaultCommands.txt"));
LstCommands.Items.Clear();
LstCommands.SelectionMode = SelectionMode.None;
LstCommands.Visible = true;
foreach (string command in commands)
{
LstCommands.Items.Add(command);
}
}
if (speech == "what time is it")
{
John.SpeakAsync(DateTime.Now.ToString("yyyyMMddTHH:mm"));
}
if (speech == "stop talking")
{
John.SpeakAsyncCancelAll();
ranNum = rnd.Next(1,2);
if (ranNum == 1)
{
John.SpeakAsync("Yes sir");
}
if (ranNum == 2)
{
John.SpeakAsync("I am Sorry I will be quiet");
}
}
if (speech == "stop listening")
{
John.SpeakAsync("Very well sir");
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
if (speech == "hide commands")
{
LstCommands.Visible = false;
}
}
private void _recognizer_SpeechRecognized(object sender,SpeechDetectedEventArgs e)
{
RecTimeOut = 0;
}
private void StartListening_SpeechRecognized(object sender,SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
if (speech == "wake up")
{
StartListening.RecognizeAsyncCancel();
John.SpeakAsync("Yes,I'm here");
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
}
private void TmrSpeaking_Tick(object sender,EventArgs e)
{
if (RecTimeOut == 10)
{
_recognizer.RecognizeAsyncCancel();
}
else if (RecTimeOut == 11)
{
TmrSpeaking.Stop();
StartListening.RecognizeAsync(RecognizeMode.Multiple);
RecTimeOut = 0;
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)