2013-07-18 61 views
1

我的應用多次撥打SpeechSynthesizer.SpeakTextAsync,所以大部分文字都會在發言前加入隊列。我想讓用戶能夠取消發言並丟棄仍然在隊列中的任何東西。取消Windows Phone SpeechSynthesizer

我試過調用SpeechSynthesizer.CancelAllSpeechSynthesizer.Dispose,當任何一個方法被調用時,應用程序都會崩潰。

我已經看過Cancel speech synthesis in windows phone 8,但是由於我的應用在隊列中添加了多個語音,因此Task.Cancel似乎不起作用。

+0

@會,它的工作!下次更仔細地閱讀文件。你能寫一個答案,所以我可以將其標記爲答案?謝謝! – Jim

+0

完成。如果你編輯並添加一小段代碼給我的答案,那就證明我不會哭泣。 – Will

回答

4

恩,according to the documentation,當您撥打CancellAll時,您將取消正在異步執行的Task。根據合同,這導致OperationCancelledException被拋出。這意味着無論您在哪裏調用SpeakTextAsync,SpeakSsmlAsync或SpeakSsmlFromUriAsync,都必須使用try/catch語句來包圍這些調用,以防止此異常未被捕獲。

+0

這很好。謝謝 –

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

它完成了!請享用...!