0

I can't seem to find any solutions to my problem anywhere.

I am trying to use SpeechSynthesizer in my application but anything that I try (including code from the Microsoft documentation) but nothing seems to work. The code that I am using is:

using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    // Configure the audio output.   
    synth.SetOutputToDefaultAudioDevice();

    // Speak a string synchronously.  
    synth.Speak("test");
}

I am using the following namespaces

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
using Process = System.Diagnostics.Process;
using System.Speech.Recognition;
using System.Media;
using System.IO;
using System.Speech.Synthesis;

I got the code from the Microsoft .NET documentation here: LINK

The code throws a System.NullReferenceException at synth.SetOutputToDefaultAudioDevice(); and when I comment the line out it throws it here too synth.Speak("test");

The stack trace says that it is from the System.Speech dll or at least I think it is anyway; I'm not sure. A link to the stack trace is:

   at System.Speech.Internal.ObjectTokens.RegistryDataKey.HKEYfromRegKey(RegistryKey regKey)
   at System.Speech.Internal.ObjectTokens.RegistryDataKey.RootHKEYFromRegPath(String rootPath)
   at System.Speech.Internal.ObjectTokens.RegistryDataKey.Open(String registryPath, Boolean fCreateIfNotExist)
   at System.Speech.Internal.ObjectTokens.ObjectTokenCategory.Create(String sCategoryId)
   at System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut()
   at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer)
   at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
   at System.Speech.Synthesis.SpeechSynthesizer.get_Voice()
   at SampleSynthesis.Program.Main(String[] args) in C:\Users\Sonic\Desktop\test\Program.cs:line 26

There appear to be other questions that are related to this, and so my question does not appear to be a simple duplicate of other NRE related questions. Here are some similar questions that may help provide an answer:

Kit
  • 20,354
  • 4
  • 60
  • 103
Soniczac7
  • 21
  • 5
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch May 26 '21 at 23:10
  • Click on the magnifying glass for StackTrace and post the text instead. You likely didn't setup something, which causes the methods on `synth` to misbehave. – Kit May 27 '21 at 18:35
  • Sorry I completely forgot to provide a stacktrace. It may have helped with a solution. I have edited the question with the stacktrace. However, it did help me understand what I was up against I still haven't found a solution. (for the comment by Dour High Arch) – Soniczac7 May 27 '21 at 18:46
  • @Kit I have edited the post with the magnifying glass version of the stacktrace. – Soniczac7 May 27 '21 at 18:51
  • I'm loathe to post an answer, but it appears you're in "good company" as far as this issue. Searching **HKEYfromRegKey** yields several NRE and synthesizer related results. It may be as simple as missing an expected key in the Windows registry. – Kit May 27 '21 at 20:21
  • This question is not simply an NRE duplicate, and shouldn't be closed, as there are at least two SO questions (unresolved) that are similar. That means a real solution would be useful. – Kit May 27 '21 at 20:23

1 Answers1

1

It took a while but I found an answer. I didn't have System.Speech installed in the Manage NuGet Packages section. I just added the DLL from Add a project reference but it was not installed so it was throwing the NullReferenceException. For anyone else who has this problem you must install the NuGet package by going to Project > Manage NuGet Packages > Browse then search System.Speech in the search bar, click on the package with the .NET icon and press Install.

Also thanks for everyone who tried to help :)

Soniczac7
  • 21
  • 5