如何让C#中的EventHandler(SpeakCompleted)工作?(System.Speech)

问题描述 投票:0回答:1

简单的说,我想做一个倒计时的程序(+语音),但当数字如1mil出现时,需要超过1秒的时间来发音,所以我想知道如何让 "EventHandler "运行,以及如何使用它(我不需要计数的代码等,但如何创建一个EventHandler,以及当它被调用时,我需要在哪里写代码)。

https:/docs.microsoft.comen-usdotnetapisystem.speech.synthesizer.speechcompleted?view=netframework-4.8。

我不能只用 Text.Speak("") 因为这将导致与文本打印的desync。我需要这个回调来启动一个新的Speak并与文本同步。

对不起......我不想问大家,但3小时后我投降了,请大家帮助我。

        SpeechSynthesizer synth = new SpeechSynthesizer();
        int counting = 0;
        private void TTS() //First trigger
        {
            synth.SetOutputToDefaultAudioDevice();
            synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.NotSet, 0);
            textBox1.Text = "1";
            synth.Speak("1");
            counting = 1;
            synth.SpeakCompleted += synth_SpeechOver;
        }
        //public event EventHandler<System.Speech.Synthesis.SpeakCompletedEventArgs> SpeakCompleted; deleted
        public void synth_SpeechOver(object sender, EventArgs e)
        {
            synth.SetOutputToDefaultAudioDevice();
            synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.NotSet, 0);
            counting++;
            synth.Speak(counting.toString());
        }
        void Form1_SpeakCompleted(object sender, EventArgs e)
        {

        }
        //(Form1_SpeakCompleted is just for testing (doesnt work)
c# system text-to-speech
1个回答
0
投票

回调只适用于SpeakAsync

https:/docs.microsoft.comen-usdotnetapisystem.speech.synthesizer.speechcompleted?view=netframework-4.8。

SpeechSynthesizer在任何一个SpeakAsync或SpeakSsmlAsync方法完成时都会引发SpeakCompleted事件。

© www.soinside.com 2019 - 2024. All rights reserved.