取消Windows Phone SpeechSynthesizer

时间:2013-07-18 15:20:35

标签: windows-phone-8 text-to-speech

我的应用多次调用SpeechSynthesizer.SpeakTextAsync,因此大部分文字都会在发言前添加到队列中。我想让用户能够取消演讲并丢弃仍在队列中的eveyything。

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

我查看了Cancel speech synthesis in windows phone 8,但由于我的应用会在队列中添加多个语音,Task.Cancel似乎无效。

2 个答案:

答案 0 :(得分:4)

好吧,according to the documentation,当你致电CancellAll时,你正在取消异步执行的Task。通过合同,这会导致OperationCancelledException被抛出。这意味着无论您何时调用SpeakTextAsync,SpeakSsmlAsync或SpeakSsmlFromUriAsync,都必须使用try / catch语句包围这些调用,以防止此异常被捕获。

答案 1 :(得分:0)

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()

完成了!享受... ...!