C# 中的空白表单使用 speech.recognition
Blank Form in C# using speech.recognition
嗨,我遇到了这种情况,我不知道如何解决。这段代码很好,错误为 0,但每当我执行 F5 时,它都会显示一个空白表格,没有声音,也无法识别,请帮忙。请我真的需要帮助有人可以帮助我吗?
现在这条线只适用于 "it look like your post is mostly code; please add some more details";
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;
namespace a.i
{
public partial class Form1 : Form
{
SpeechSynthesizer s = new SpeechSynthesizer();
Choices list = new Choices();
public Form1()
{
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
list.Add(new String[] { "hello", "how are you" });
Grammar gr = new Grammar(new GrammarBuilder(list));
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_SpeachRecognized;
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch { return; }
s.SelectVoiceByHints(VoiceGender.Neutral);
s.Speak("Hello, my name is Gabriel ChatterBot");
InitializeComponent();
}
public void say(string h)
{
s.Speak(h);
}
private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
{
String r = e.Result.Text;
//what you say
if (r == "hello")
{
// what it says
say("hi");
}
//what you say
if (r == "how are you")
{
// what it says
say("Great, and you!");
}
}
}
}
从您发布的异常来看,我认为您没有正确初始化 Grammar 和 SpeechRecognitionEngine。看来您需要为其指定一种语言/文化。从文档中:
https://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx
// Create a new SpeechRecognitionEngine instance.
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
嗨,我遇到了这种情况,我不知道如何解决。这段代码很好,错误为 0,但每当我执行 F5 时,它都会显示一个空白表格,没有声音,也无法识别,请帮忙。请我真的需要帮助有人可以帮助我吗?
现在这条线只适用于 "it look like your post is mostly code; please add some more details";
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;
namespace a.i
{
public partial class Form1 : Form
{
SpeechSynthesizer s = new SpeechSynthesizer();
Choices list = new Choices();
public Form1()
{
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
list.Add(new String[] { "hello", "how are you" });
Grammar gr = new Grammar(new GrammarBuilder(list));
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_SpeachRecognized;
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch { return; }
s.SelectVoiceByHints(VoiceGender.Neutral);
s.Speak("Hello, my name is Gabriel ChatterBot");
InitializeComponent();
}
public void say(string h)
{
s.Speak(h);
}
private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
{
String r = e.Result.Text;
//what you say
if (r == "hello")
{
// what it says
say("hi");
}
//what you say
if (r == "how are you")
{
// what it says
say("Great, and you!");
}
}
}
}
从您发布的异常来看,我认为您没有正确初始化 Grammar 和 SpeechRecognitionEngine。看来您需要为其指定一种语言/文化。从文档中: https://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx
// Create a new SpeechRecognitionEngine instance.
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));