0

我似乎无法在任何地方找到任何解决我的问题的方法。

我正在尝试在我的应用程序中使用 SpeechSynthesizer,但我尝试的任何东西(包括 Microsoft 文档中的代码)但似乎没有任何效果。我正在使用的代码是:

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

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

我正在使用以下命名空间

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;

我从此处的 Microsoft .NET 文档中获得了代码: LINK

该代码会抛出一个 System.NullReferenceException ,synth.SetOutputToDefaultAudioDevice();当我注释掉该行时,它也会在这里抛出它synth.Speak("test");

堆栈跟踪表明它来自 System.Speech dll,或者至少我认为它是无论如何;我不知道。堆栈跟踪的链接是:

   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

似乎还有其他与此相关的问题,因此我的问题似乎不是其他 NRE 相关问题的简单重复。以下是一些可能有助于提供答案的类似问题:

4

1 回答 1

1

花了一段时间,但我找到了答案。我没有在“管理 NuGet 包”部分安装 System.Speech。我刚刚添加了 DLL,Add a project reference但它没有安装,所以它抛出了 NullReferenceException。对于遇到此问题的其他人,您必须安装 NuGet 包,Project > Manage NuGet Packages > Browse然后System.Speech在搜索栏中搜索,单击带有 .NET 图标的包,然后按安装。

也感谢所有试图帮助的人:)

于 2021-06-28T15:13:45.463 回答