Reputation: 1735
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.
Upvotes: 1
Views: 408
Reputation: 1880
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...!
Upvotes: 0
Reputation:
Well, according to the documentation, when you call CancellAll
, you're cancelling the Task
s 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.
Upvotes: 4