首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET SpeechSynthesizer .NET System.NullReferenceException

.NET SpeechSynthesizer .NET System.NullReferenceException
EN

Stack Overflow用户
提问于 2021-05-26 18:48:55
回答 1查看 364关注 0票数 0

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

我试图在我的应用程序中使用SpeechSynthesizer,但是我尝试的任何东西(包括来自微软文档的代码)似乎都不起作用。我使用的代码是:

代码语言:javascript
复制
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
    // Configure the audio output.   
    synth.SetOutputToDefaultAudioDevice();

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

我使用以下名称空间

代码语言:javascript
复制
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,或者至少我认为它是来自;我不确定。指向堆栈跟踪的链接是:

代码语言:javascript
复制
   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相关问题的简单重复。以下是一些类似的问题,它们可能有助于提供一个答案:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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。

也感谢每一个想帮忙的人:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67711037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档