我似乎在任何地方都找不到解决问题的办法。
我试图在我的应用程序中使用SpeechSynthesizer,但是我尝试的任何东西(包括来自微软文档的代码)似乎都不起作用。我使用的代码是:
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文档获得了以下代码:链接
代码在synth.SetOutputToDefaultAudioDevice();处抛出一个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相关问题的简单重复。以下是一些类似的问题,它们可能有助于提供一个答案:
发布于 2021-06-28 15:13:45
花了一段时间,但我找到了答案。我在Manage NuGet Packages部分中没有安装NuGet。我刚从Add a project reference中添加了DLL,但是它没有安装,所以它抛出了NullReferenceException。对于其他有此问题的人,您必须安装NuGet包,方法是转到Project > Manage NuGet Packages > Browse,然后在搜索栏中搜索System.Speech,单击带有.NET图标的包,然后按Install。
也感谢每一个想帮忙的人:)
https://stackoverflow.com/questions/67711037
复制相似问题