首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows Phone 8 SpeechSynthesizer暂停

Windows Phone 8 SpeechSynthesizer暂停
EN

Stack Overflow用户
提问于 2013-03-27 01:51:05
回答 2查看 976关注 0票数 1

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

代码语言:javascript
复制
await synthesizer.SpeakTextAsync(text);

var stop = true;出现时停止阅读

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-27 03:40:43

前段时间有人在这里发帖,同时我刷新了页面,阅读了他的答案,看到一条通知,又刷新了页面&答案不见了。但无论是谁发布的,他都是救命稻草。它卡在我的脑海里,我最终创造了这个。

代码语言:javascript
复制
    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();
    }

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

票数 1
EN

Stack Overflow用户

发布于 2016-08-22 19:39:49

根据文档,当您调用CancellAll时,您将取消异步执行的任务。根据约定,这会导致抛出OperationCancelledException。这意味着,无论您在何处调用SpeakTextAsync、SpeakSsmlAsync或SpeakSsmlFromUriAsync,都必须使用try/catch语句将这些调用括起来,以防止此异常无法捕获。

示例:

代码语言:javascript
复制
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()即可。

完成了!尽情享受吧!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15644054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档