我的应用程序多次调用SpeechSynthesizer.SpeakTextAsync,因此大多数文本将在朗读之前添加到队列中。我想让用户能够取消语音并丢弃队列中仍然存在的所有内容。
我尝试调用SpeechSynthesizer.CancelAll或SpeechSynthesizer.Dispose,当调用这两个方法中的任何一个时,应用程序都会崩溃。
我看过Cancel speech synthesis in windows phone 8,但由于我的应用程序向队列中添加了多个语音,Task.Cancel似乎不起作用。
发布于 2013-07-19 03:23:40
好的,according to the documentation,当你调用CancellAll时,你正在取消异步执行的Task。根据约定,这会导致抛出OperationCancelledException。这意味着,无论您在何处调用SpeakTextAsync、SpeakSsmlAsync或SpeakSsmlFromUriAsync,都必须使用try/catch语句将这些调用括起来,以防止此异常无法捕获。
发布于 2016-08-22 19:42:24
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()即可。
完成了!尽情享受吧!
https://stackoverflow.com/questions/17727545
复制相似问题