在提出原始问题 3 年后,API 似乎包含相同的问题,所以这里有一个更“深入”的答案。
TL;DR;代码示例 - 在底部
语音列表的问题是 Microsoft Speech API 的奇怪设计 - Windows 中有两组语音在注册表的不同位置注册 - 一组在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices,另一个 - 在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices。
问题在于 SpeechSynthesizer 的(或者更具体地说 - VoiceSynthesis 的)初始化例程对于第一个例程是 nailed,同时我们通常需要两者的组合。
所以,实际上有两种方法可以克服这种行为。
选项 1(在其他答案中提到的那个):操纵注册表以物理复制 Speech_OneCore 注册表中的语音定义记录,使其对 SpeechSynthesizer 可见。在这里您有很多选择:手动注册表操作、PowerShell 脚本、基于代码等。
选项 2(我在项目中使用的那个):使用反射将额外的声音放入内部 VoiceSyntesis 的 _installedVoices 字段,有效模拟微软在他们的代码。
好消息是 Speech API 源代码现已开放,因此我们不必在黑暗中摸索以了解我们需要做什么。
这是original code sn-p:
using (ObjectTokenCategory category = ObjectTokenCategory.Create(SAPICategories.Voices))
{
if (category != null)
{
// Build a list with all the voicesInfo
foreach (ObjectToken voiceToken in category.FindMatchingTokens(null, null))
{
if (voiceToken != null && voiceToken.Attributes != null)
{
voices.Add(new InstalledVoice(voiceSynthesizer, new VoiceInfo(voiceToken)));
}
}
}
}
我们只需要将 SAPICategories.Voices 常量替换为另一个注册表项路径并重复整个配方。
坏消息是这里使用的所有需要的类、方法和字段都是内部的,因此我们必须广泛使用反射来实例化类、调用方法和获取/设置字段。
请在下面找到我的实现示例 - 您在合成器上调用 InjectOneCoreVoices 扩展方法,它就完成了这项工作。请注意,如果出现问题,它会抛出异常,所以不要忘记正确的 try/catch 环境。
public static class SpeechApiReflectionHelper
{
private const string PROP_VOICE_SYNTHESIZER = "VoiceSynthesizer";
private const string FIELD_INSTALLED_VOICES = "_installedVoices";
private const string ONE_CORE_VOICES_REGISTRY = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices";
private static readonly Type ObjectTokenCategoryType = typeof(SpeechSynthesizer).Assembly
.GetType("System.Speech.Internal.ObjectTokens.ObjectTokenCategory")!;
private static readonly Type VoiceInfoType = typeof(SpeechSynthesizer).Assembly
.GetType("System.Speech.Synthesis.VoiceInfo")!;
private static readonly Type InstalledVoiceType = typeof(SpeechSynthesizer).Assembly
.GetType("System.Speech.Synthesis.InstalledVoice")!;
public static void InjectOneCoreVoices(this SpeechSynthesizer synthesizer)
{
var voiceSynthesizer = GetProperty(synthesizer, PROP_VOICE_SYNTHESIZER);
if (voiceSynthesizer == null) throw new NotSupportedException($"Property not found: {PROP_VOICE_SYNTHESIZER}");
var installedVoices = GetField(voiceSynthesizer, FIELD_INSTALLED_VOICES) as IList;
if (installedVoices == null)
throw new NotSupportedException($"Field not found or null: {FIELD_INSTALLED_VOICES}");
if (ObjectTokenCategoryType
.GetMethod("Create", BindingFlags.Static | BindingFlags.NonPublic)?
.Invoke(null, new object?[] {ONE_CORE_VOICES_REGISTRY}) is not IDisposable otc)
throw new NotSupportedException($"Failed to call Create on {ObjectTokenCategoryType} instance");
using (otc)
{
if (ObjectTokenCategoryType
.GetMethod("FindMatchingTokens", BindingFlags.Instance | BindingFlags.NonPublic)?
.Invoke(otc, new object?[] {null, null}) is not IList tokens)
throw new NotSupportedException($"Failed to list matching tokens");
foreach (var token in tokens)
{
if (token == null || GetProperty(token, "Attributes") == null) continue;
var voiceInfo =
typeof(SpeechSynthesizer).Assembly
.CreateInstance(VoiceInfoType.FullName!, true,
BindingFlags.Instance | BindingFlags.NonPublic, null,
new object[] {token}, null, null);
if (voiceInfo == null)
throw new NotSupportedException($"Failed to instantiate {VoiceInfoType}");
var installedVoice =
typeof(SpeechSynthesizer).Assembly
.CreateInstance(InstalledVoiceType.FullName!, true,
BindingFlags.Instance | BindingFlags.NonPublic, null,
new object[] {voiceSynthesizer, voiceInfo}, null, null);
if (installedVoice == null)
throw new NotSupportedException($"Failed to instantiate {InstalledVoiceType}");
installedVoices.Add(installedVoice);
}
}
}
private static object? GetProperty(object target, string propName)
{
return target.GetType().GetProperty(propName, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(target);
}
private static object? GetField(object target, string propName)
{
return target.GetType().GetField(propName, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(target);
}
}