1

我正在开发一个应用程序,我想在 SpeechSynthesizer.SpeakTextAsync 运行时暂停并从那里恢复。

await synthesizer.SpeakTextAsync(text);

什么时候停止阅读var stop = true;

4

2 回答 2

1

不久前有人在这里发帖,同时我刷新了页面,阅读了他的答案,看到了通知并再次刷新了页面,答案消失了。但无论是谁发帖,他都是救命稻草。它让我想起了,我最终创造了这个。

    String text;  // your text to read out loud
    String[] parts = text.Split(' ');
    int max = parts.Length;
    int count = 0;

    private String makeSSML() {
        if (count == max) { 
            count= 0;
        }
        String s = "<speak version=\"1.0\" ";
        s += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"en-US\">";
        for (int i = count; i < max; i++)
        {
            s += parts[i];
            s += "<mark name=\"anything\"/>";
        }
        s += "<mark name=\"END\"/>";
        s += "</speak>";
        return s;
    }

    private void playIT(){
        synth = new SpeechSynthesizer();
        synth.BookmarkReached += synth_BookmarkReached;
        synth.SpeakSsmlAsync(makeSSML());
    }

    private void synth_BookmarkReached(object sender, SpeechBookmarkReachedEventArgs e)
    {
        count++;
        if (e.Bookmark == "END") {
            synth.Dispose();
        }
    }

    private void Pause_Click(object sender, RoutedEventArgs e)
    {
        synth.Dispose();
    }

谢谢大佬,你的回答给了我灵感。

于 2013-03-26T19:40:42.757 回答
0

好吧,根据文档,当您调用 时CancellAll,您正在取消异步执行的任务。根据合同,这会导致OperationCancelledException被抛出。这意味着无论您调用 SpeakTextAsync、SpeakSsmlAsync 还是 SpeakSsmlFromUriAsync,都必须在这些调用周围加上 try/catch 语句,以防止此异常未被捕获。

例子:

private static SpeechSynthesizer synth;

public async static Task<SpeechSynthesizer> SpeechSynth(string dataToSpeak)
        {
            synth = new SpeechSynthesizer();
            IEnumerable<VoiceInformation> englishVoices = from voice in InstalledVoices.All
                                                          where voice.Language == "en-US"
                                                          && voice.Gender.Equals(VoiceGender.Female)
                                                          select voice;
            if (englishVoices.Count() > 0)
            {
                synth.SetVoice(englishVoices.ElementAt(0));
            }

            await synth.SpeakTextAsync(dataToSpeak); 

            return synth;
        }  


public static void CancelSpeech()
        {
            synth.CancelAll();
        }

现在调用SpeechSynth("Some Data to Speak")你想要的地方,只要你想取消它,只需调用CancelSpeech().

完成!享受...!

于 2016-08-22T11:39:48.860 回答