使用 RecognizeAsync 启动识别器时出现 NullReferenceException
NullReferenceException when starting recognizer with RecognizeAsync
嗨,我目前正在开发一个程序,该程序应该可以识别我的声音,然后写下它听到的内容,但是当我 运行 代码并单击按钮启动 recEngine 时,它说"An unhandled exception of type 'System.NullReferenceException' occurred in System.Speech.dll",并且在提示中说有可能之前的var为null。但是我之前设置过变量还是我做错了什么。
这是代码
using System;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace Voice_Recognition
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
}
private void BtnEnable_Click(object sender, EventArgs e)
{
这是出现错误的行。
recEngine.RecognizeAsync(RecognizeMode.Multiple);
// BtnEnable.Enabled = false;
BtnDisable.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Choices commands = new Choices();
commands.Add(new string[] { "say hello", "print my name" });
GrammarBuilder Gbuilder = new GrammarBuilder();
Gbuilder.Append(commands);
Grammar grammar = new Grammar(Gbuilder);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "say hello":
Log.Text += "\nHello";
break;
case "print my name":
Log.Text += "\nMyname";
break;
}
}
private void BtnDisable_Click(object sender, EventArgs e)
{
BtnEnable.Enabled = true;
BtnDisable.Enabled = false;
recEngine.RecognizeAsyncStop();
}
}
}
在提示菜单中它说:
-调用方法前检查对象是否为空
-使用"new"关键字创建对象实例
提前致谢
一些看起来很奇怪的事情:
使用recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
代替recEngine.SpeechRecognized += recEngine_SpeechRecognized;
可能是你开始识别的时候语法还没有加载完成;使用 LoadGrammar
而不是 LoadGrammarAsync
(或为 LoadGrammarCompleted
添加处理程序)。
我一直发现明确指定识别器(和语法)的文化信息很方便,因为这可以防止对使用的语言产生误解。
嗨,我目前正在开发一个程序,该程序应该可以识别我的声音,然后写下它听到的内容,但是当我 运行 代码并单击按钮启动 recEngine 时,它说"An unhandled exception of type 'System.NullReferenceException' occurred in System.Speech.dll",并且在提示中说有可能之前的var为null。但是我之前设置过变量还是我做错了什么。
这是代码
using System;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace Voice_Recognition
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
}
private void BtnEnable_Click(object sender, EventArgs e)
{
这是出现错误的行。
recEngine.RecognizeAsync(RecognizeMode.Multiple);
// BtnEnable.Enabled = false;
BtnDisable.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
Choices commands = new Choices();
commands.Add(new string[] { "say hello", "print my name" });
GrammarBuilder Gbuilder = new GrammarBuilder();
Gbuilder.Append(commands);
Grammar grammar = new Grammar(Gbuilder);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "say hello":
Log.Text += "\nHello";
break;
case "print my name":
Log.Text += "\nMyname";
break;
}
}
private void BtnDisable_Click(object sender, EventArgs e)
{
BtnEnable.Enabled = true;
BtnDisable.Enabled = false;
recEngine.RecognizeAsyncStop();
}
}
}
在提示菜单中它说:
-调用方法前检查对象是否为空
-使用"new"关键字创建对象实例
提前致谢
一些看起来很奇怪的事情:
使用
recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
代替recEngine.SpeechRecognized += recEngine_SpeechRecognized;
可能是你开始识别的时候语法还没有加载完成;使用
LoadGrammar
而不是LoadGrammarAsync
(或为LoadGrammarCompleted
添加处理程序)。我一直发现明确指定识别器(和语法)的文化信息很方便,因为这可以防止对使用的语言产生误解。