我似乎无法在任何地方找到任何解决我的问题的方法。
我正在尝试在我的应用程序中使用 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 相关问题的简单重复。以下是一些可能有助于提供答案的类似问题: