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

取消Windows Phone SpeechSynthesizer
EN

Stack Overflow用户
提问于 2013-07-18 23:20:35
回答 2查看 355关注 0票数 1

我的应用程序多次调用SpeechSynthesizer.SpeakTextAsync,因此大多数文本将在朗读之前添加到队列中。我想让用户能够取消语音并丢弃队列中仍然存在的所有内容。

我尝试调用SpeechSynthesizer.CancelAllSpeechSynthesizer.Dispose,当调用这两个方法中的任何一个时,应用程序都会崩溃。

我看过Cancel speech synthesis in windows phone 8,但由于我的应用程序向队列中添加了多个语音,Task.Cancel似乎不起作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-19 03:23:40

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

票数 4
EN

Stack Overflow用户

发布于 2016-08-22 19:42:24

代码语言: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/17727545

复制
相关文章

相似问题

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