我在 Unity 项目中使用 C# SpeechSynthesizer,如下例所示:
我收到此错误:
NullReferenceException: Object reference not set to an instance of an object
System.Speech.Internal.ObjectTokens.RegistryDataKey.HKEYfromRegKey (Microsoft.Win32.RegistryKey regKey) (at [...])
System.Speech.Internal.ObjectTokens.RegistryDataKey.RootHKEYFromRegPath (System.String rootPath) (at [...])
System.Speech.Internal.ObjectTokens.RegistryDataKey.Open (System.String registryPath, System.Boolean fCreateIfNotExist) (at [...])
System.Speech.Internal.ObjectTokens.ObjectTokenCategory.Create (System.String sCategoryId) (at [...])
System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut () (at [...])
System.Speech.Internal.Synthesis.VoiceSynthesis..ctor (System.WeakReference speechSynthesizer) (at [...])
System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer () (at [...])
System.Speech.Synthesis.SpeechSynthesizer.SelectVoiceByHints (System.Speech.Synthesis.VoiceGender gender, System.Speech.Synthesis.VoiceAge age, System.Int32 voiceAlternate, System.Globalization.CultureInfo culture) (at [...])
System.Speech.Synthesis.SpeechSynthesizer.SelectVoiceByHints (System.Speech.Synthesis.VoiceGender gender) (at [...])
这是我的课:
using System;
using System.Speech.Synthesis;
using UnityEngine;
public class Speaker : MonoBehaviour
{
private SpeechSynthesizer synth = new SpeechSynthesizer();
void Start() {
synth.SetOutputToDefaultAudioDevice();
synth.SelectVoiceByHints(VoiceGender.Female);
}
}
欢迎任何想法。