tags:

views:

78

answers:

2

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;

    }
+1  A: 

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.

ChrisF
+1  A: 

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.

Rob