1

My app is calling SpeechSynthesizer.SpeakTextAsync multiple time, so most of the text will be add to the queue before spoken. I want to give user the ability to cancel the speech and discard eveyything that's still in the queue.

I tried calling either SpeechSynthesizer.CancelAll or SpeechSynthesizer.Dispose and the app will just crash when either of the methods were called.

I've looked at Cancel speech synthesis in windows phone 8 but since my app add multiple speech to the queue, Task.Cancel doesn't seem to work.

4

2 回答 2

4

Well, according to the documentation, when you call CancellAll, you're cancelling the Tasks that are executing asynchronously. By contract, this results in an OperationCancelledException being thrown. That means that wherever you call SpeakTextAsync, SpeakSsmlAsync or SpeakSsmlFromUriAsync, you must surround these calls with a try/catch statement to prevent this exception from going uncaught.

于 2013-07-18T19:23:39.560 回答
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();
        }

Now call the SpeechSynth("Some Data to Speak") where you want, and whenever you want to cancel it, just call CancelSpeech().

Its Done! Enjoy...!

于 2016-08-22T11:42:24.423 回答