Unity中使用SpeechLib播放语音

	public class SpeechTest : MonoBehaviour {

        Thread t;
        SpVoice spVoice;
        string DefaultEnglishLangID = "804";//中文 409:英文
		
		void Update () {
            if (Input.GetKeyDown(KeyCode.S))
            {
                t = null;
                SpeakVoice("哈哈,正义必胜,hello world");
            }
		}
        public void SpeakVoice(string content)
        {
            try
            {
                if (t==null)
                {
                    t = new Thread(() => 
                    {
                        string contentStr = "" + content + "";

                        if (spVoice == null)
                        {
                            spVoice = new SpVoice();
                            //spVoice.Voice = spVoice.GetVoices(string.Empty, string.Empty).Item(0);
                            spVoice.Speak(contentStr, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFlagsAsync);
                        }
                        else
                        {
                            spVoice.Speak(contentStr, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFlagsAsync);
                        }
                    });
                }
                t.Start();
            }
            catch (System.Exception e)
            {
                Debug.Log(e);
            }
        }
	}

如果不能正常播放,先下载“朗读女”这个软件,安装后,断网测试看能否播放,如果不能,说明系统缺少组件。百度搜索TTS修复工具(最好是官方的)修复即可。

你可能感兴趣的:(Unity)