【问题标题】:Cancel Windows Phone SpeechSynthesizer取消 Windows Phone 语音合成器
【发布时间】:2016-08-22 19:42:24
【问题描述】:

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

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

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

【问题讨论】:

  • @Will,它成功了!下次得更仔细地阅读文件。你能写一个答案,以便我可以将其标记为答案吗?谢谢!
  • 完成。如果您在我的答案中编辑并添加一小段代码来演示,我不会哭。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    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()

    完成了!享受...!

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多