C#程序异常
该程序抛出异常,我该如何解决这个问题?
例外情况是“语音识别在此系统上不可用。无法找到 SAPI 和语音识别引擎”。
public partial class Form1 : Form
{
SpeechRecognizer rec = new SpeechRecognizer();
public Form1()
{
InitializeComponent();
rec.SpeechRecognized += rec_SpeechRecognized;
}
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
lblLetter.Text = e.Result.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
var c = new Choices();
for (var i = 0; i <= 100; i++)
c.Add(i.ToString());
var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}
This program is throwing an exception, how can I resolve this?
Exception is "Speech Recognition is not available on this system. SAPI and Speech Recognition engines cannot be found".
public partial class Form1 : Form
{
SpeechRecognizer rec = new SpeechRecognizer();
public Form1()
{
InitializeComponent();
rec.SpeechRecognized += rec_SpeechRecognized;
}
void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
lblLetter.Text = e.Result.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
var c = new Choices();
for (var i = 0; i <= 100; i++)
c.Add(i.ToString());
var gb = new GrammarBuilder(c);
var g = new Grammar(gb);
rec.LoadGrammar(g);
rec.Enabled = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您的系统上没有安装必要的组件,或者您可能没有在完全可信的应用程序中运行:
来源
虽然我想你会得到一个不同的错误。是所有机器都会出现这个问题还是只有一两台机器出现这个问题?如果是后者,则表明所需的组件未安装。
尝试将
SpeechRecognizer
对象的初始化移至表单构造函数中,并将其包装在try ... catch
块中。这将 a) 告诉您是否确实是这个问题造成的,b) 让您能够顺利恢复。Well it sounds like you don't have the necessary components installed on your system or possibly you are not running in a full trusted application:
Source
Though I would have thought you would get a different error from that. Does this problem occur on all machines or just one or two? If it's the latter then that would point to it being the required components not being installed.
Try moving the initialisation of your
SpeechRecognizer
object into the form constructor and wrapping it in atry ... catch
block. That will a) tell you whether it really is this that's causing the problem and b) allow you to recover gracefully.您尝试在什么操作系统上运行它?我已在 Windows 7 Professional 上成功执行了您的代码。
我的怀疑是,虽然框架内提供了 System.Speech,但 SAPI/语音识别引擎并未随其安装,尽管它们默认随 Windows 7 一起提供。查看您的
windows\system32
文件夹,看看是否有名为Speech
的子文件夹来确定这一点。我刚刚使用 Reflector 快速浏览了 System.Speech.dll,并且 System.Speech.Recognition.SpeechRecognizer 的构造函数最终调用并实例化一个类名为
System.Speech.Internal.SapiInterop.SapiRecognizer
,这清楚地表明您需要安装非托管组件。尝试下载并安装 语音 SDK 5.1。
What operating system are you attempting to run this on? I've executed your code successfully on Windows 7 Professional.
My suspicion is that whilst
System.Speech
is provided within the framework, the SAPI/Speech Recognition engines aren't installed with it, though they come with Windows 7 by default. Take a look in yourwindows\system32
folder and see if there's a sub-folder calledSpeech
to determine this.I've just had a quick look through
System.Speech.dll
using Reflector and the constructor forSystem.Speech.Recognition.SpeechRecognizer
eventually calls down into, and instantiates a class calledSystem.Speech.Internal.SapiInterop.SapiRecognizer
, which makes it quite clear that you need to have the unmanaged components installed.Try downloading and installing the Speech SDK 5.1.