C# 语音识别中的选择
我正在创建一个类似于《钢铁侠》中贾维斯的基于语音识别的程序。我在使用之前做了一个:C# 语音识别中的选择,c#,speech-recognition,C#,Speech Recognition,我正在创建一个类似于《钢铁侠》中贾维斯的基于语音识别的程序。我在使用之前做了一个: case "Open facebook": JARVIS.Speak("Opening facebook"); Process.Start("www.facebook.com"); break;` 但现在我想创建搜索选项,甚至播放。到目前为止,我为youtube搜索制作了这个(一个语法,效果很好),但当我制作了两个: public partial class Form1 : Form {
case "Open facebook":
JARVIS.Speak("Opening facebook");
Process.Start("www.facebook.com");
break;`
但现在我想创建搜索选项,甚至播放。到目前为止,我为youtube搜索制作了这个(一个语法,效果很好),但当我制作了两个:
public partial class Form1 : Form
{
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
SpeechSynthesizer JARVIS = new SpeechSynthesizer();
string QEvent;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Choices artists = new Choices(new string[] { "bullet-for-my-valentine-curses", "black-veil-brides-saviour", "three-days-grace-wake-up" });
Choices search = new Choices(new string[] { "bill-gates" });
GrammarBuilder findServices = new GrammarBuilder("Play");
findServices.Append(artists);
GrammarBuilder google = new GrammarBuilder("Look");
google.Append("for");
google.Append(search);
// Create a Grammar object from the GrammarBuilder and load it to the recognizer.
Grammar servicesGrammar = new Grammar(findServices);
Grammar lookingGrammar = new Grammar(google);
_recognizer.RequestRecognizerUpdate();
_recognizer.LoadGrammarAsync(servicesGrammar);
_recognizer.LoadGrammarAsync(lookingGrammar);
// Add a handler for the speech recognized event.
_recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
// Configure the input to the speech recognizer.
_recognizer.SetInputToDefaultAudioDevice();
// Start asynchronous, continuous speech recognition.
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
JARVIS.Speak("Playing" + e.Result.Words[1].Text);
Process.Start("http://www.youtube.com/results?search_query=" + e.Result.Words[1].Text);
JARVIS.Speak("Searching" + e.Result.Words[2].Text + " " + e.Result.Words[3].Text);
}
}
那么,我如何制作多个语法,一个用于谷歌搜索,一个用于维基百科,并保留这一个呢?我要借用陈雷蒙的心理调试天赋(tm),并说你的问题在这里:
_recognizer.LoadGrammarAsync(servicesGrammar);
_recognizer.LoadGrammarAsync(lookingGrammar);
特别是,我怀疑识别器一次只能加载一个异步语法。如果您将代码更改为
_recognizer.LoadGrammar(servicesGrammar);
_recognizer.LoadGrammar(lookingGrammar);
或者将第二个调用放入处理程序,您的问题就会消失
但是说真的,您需要包括错误。您试过调试它吗?哪一行抛出了异常,你得到了什么类型的异常?当我调试程序时,在我创建了两个语法之后,一个用于youtube,一个用于wikipedia搜索,当我说代码中的一些东西时,比如“Play three days grace wake”(在我创建了两个语法之后)应用程序停止工作,此部分
使用。。。命名空间WindowsFormsApplication1{静态类程序{//应用程序的主入口点。[statisthread]static void main(){application.EnableVisualStyles();application.SetCompatibleTextRenderingDefault(false);application.Run(new Form1());如果您在Form1
的构造函数中设置了断点,它会被命中吗?如果您随后在调试器中单步执行代码,您能指出失败的行吗?请编辑您的问题并附加两个语法版本的代码,因为这是实际失败的代码。
{
static class Program
{
// The main entry point for the application.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //this line show error
}
}
}