2

I'm currently trying to implement some speech recognition in one of my c# project, and so I found this library : https://learn.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine?view=netframework-4.8

Providing this code as an example :

using System;  
using System.Speech.Recognition;  

namespace SpeechRecognitionApp  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  

      // Create an in-process speech recognizer for the en-US locale.  
      using (  
      SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(  
          new System.Globalization.CultureInfo("en-US")))  
      {  

        // Create and load a dictation grammar.  
        recognizer.LoadGrammar(new DictationGrammar());  

        // Add a handler for the speech recognized event.  
        recognizer.SpeechRecognized +=   
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  

        // Configure input to the speech recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  

        // Start asynchronous, continuous speech recognition.  
        recognizer.RecognizeAsync(RecognizeMode.Multiple);  

        // Keep the console window open.  
        while (true)  
        {  
          Console.ReadLine();  
        }  
      }  
    }  

    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine("Recognized text: " + e.Result.Text);  
    }  
  }  
}

Which should do exactly what I need. So I created a new project in Visual Studio, copy-pasted the code, and run it.

There is no compilation error, but the constructor of SpeechRecognitionEngine, taking a non-null CultureInfo object as an argument, throws a System.NullReferenceException in System.Speech.dll.

To try to debug this, I made sure

new System.Globalization.CultureInfo("en-US") 

returns a non-null object, and that this culture was installed. I also updated my framework to 4.8, and run the project both as administrator and normal user. I'm also using a x64 CPU platform, as the build fails with an ANY CPU platform.

It seems to me like I misconfigured something somewhere, as the code itself shouldn't be wrong.

Do you have any idea how to solve my problem ?

Thank you for your help.

EDIT : this may be linked to this problem, though I don't know if it's of any help : NullReferenceException when starting recognizer with RecognizeAsync

Astrosias
  • 23
  • 4
  • 2
    I've reopened your question. @Zohar OP's code is a verbatim copy of the documentation sample OP linked to. Furthermore, OP has done fact-finding sufficient that we know it's not a problem in their code. – ProgrammingLlama Jun 04 '20 at 07:29
  • The code itself is a good duplicate. But maybe there is something else to be added. – Astrosias Jun 04 '20 at 07:30
  • 1
    @John, that's probably a good thing you've reopened it. The NullReferenceException in the title of a c# question made it a very good candidate IMHO to be closed as a dupe, but after reading carefully the content of the question I think you might be correct. – Zohar Peled Jun 04 '20 at 07:31
  • I can't reproduce this on Win 10 x64 Enterprise N (English UK) using .NET Framework 4.7.2, built for debug. – ProgrammingLlama Jun 04 '20 at 07:34
  • Can you try rebooting, and then do a clean / rebuild on your system and try again? – ProgrammingLlama Jun 04 '20 at 07:35
  • What do you mean by clean/rebuild ? And I have this problem since yesterday, I rebooted twice since the first time it happened. – Astrosias Jun 04 '20 at 07:40
  • Build | Clean Solution, Build | Build Solution – ProgrammingLlama Jun 04 '20 at 07:41
  • I just cleaned, rebuilt, run, then rebooted, cleaned, built, run, and still I get the same error. – Astrosias Jun 04 '20 at 07:52

1 Answers1

0

I had the same error, but found the issue on my end. If you have the same issue as me, it may be that your project is using System.Speech.dll from an older framework, which I believe was causing my error. Or it may have been incompatibility between my target framework the dll?

I used NuGet to add System.Speech by Microsoft to my project. It added System.Speech (6.0.0) to the project references. This removed the null exception I was getting.

For information only, I initially added a reference to v3.0 framework, and that was causing my issue.

Tommy
  • 1
  • I have left this project long ago, but I remember having trouble with the version of the Framework used and this library. In the end, I must have given up on C# and wrote the thing in python. – Astrosias May 01 '22 at 20:36