我正在开发一个应用程序,我想在SpeechSynthesizer.SpeakTextAsync正在运行之间暂停,然后从那里恢复。
await synthesizer.SpeakTextAsync(text);
var stop = true;
答案 0 :(得分:1)
有人在这里发布了一段时间,同时我刷新了页面,阅读了他的回答,看到了通知&再次刷新页面&答案消失了。但无论谁发布,他都是一个救生员。它引起了我的注意,我最终创造了这个。
String text; // your text to read out loud
String[] parts = text.Split(' ');
int max = parts.Length;
int count = 0;
private String makeSSML() {
if (count == max) {
count= 0;
}
String s = "<speak version=\"1.0\" ";
s += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"en-US\">";
for (int i = count; i < max; i++)
{
s += parts[i];
s += "<mark name=\"anything\"/>";
}
s += "<mark name=\"END\"/>";
s += "</speak>";
return s;
}
private void playIT(){
synth = new SpeechSynthesizer();
synth.BookmarkReached += synth_BookmarkReached;
synth.SpeakSsmlAsync(makeSSML());
}
private void synth_BookmarkReached(object sender, SpeechBookmarkReachedEventArgs e)
{
count++;
if (e.Bookmark == "END") {
synth.Dispose();
}
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
synth.Dispose();
}
谢谢你,你的答案给了我这个主意。
答案 1 :(得分:0)
根据文档,当您调用CancellAll
时,您将取消异步执行的任务。通过合同,这会导致OperationCancelledException
被抛出。这意味着无论您何时调用SpeakTextAsync,SpeakSsmlAsync或SpeakSsmlFromUriAsync,都必须使用try / catch语句包围这些调用,以防止此异常被捕获。
示例:强>
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()
即可。
完成了!享受... ...!