代码之家  ›  专栏  ›  技术社区  ›  luthien

Unity中的SpeechSynthesizer出现空引用异常

  •  1
  • luthien  · 技术社区  · 5 年前

    我在Unity项目中使用C语言合成器,如以下示例所示:

    https://docs.microsoft.com/en-us/dotnet/api/system.speech.synthesis.speechsynthesizer?view=netframework-4.8

    我得到这个错误:

    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);
            }
    
          }
    

    欢迎提出任何意见。

    0 回复  |  直到 5 年前