C#程序异常

发布于 2024-09-16 23:59:08 字数 763 浏览 2 评论 0原文

该程序抛出异常,我该如何解决这个问题?

例外情况是“语音识别在此系统上不可用。无法找到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人的歌 2024-09-23 23:59:08

听起来您的系统上没有安装必要的组件,或者您可能没有在完全可信的应用程序中运行:

对直接呼叫者的完全信任。该成员不能被部分信任的代码使用。有关详细信息,请参阅使用部分受信任代码中的库。

来源

虽然我想你会得到一个不同的错误。是所有机器都会出现这个问题还是只有一两台机器出现这个问题?如果是后者,则表明所需的组件未安装。

尝试将 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:

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

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 a try ... catch block. That will a) tell you whether it really is this that's causing the problem and b) allow you to recover gracefully.

德意的啸 2024-09-23 23:59:08

您尝试在什么操作系统上运行它?我已在 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 your windows\system32 folder and see if there's a sub-folder called Speech to determine this.

I've just had a quick look through System.Speech.dll using Reflector and the constructor for System.Speech.Recognition.SpeechRecognizer eventually calls down into, and instantiates a class called System.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文