我正在开发一个应用程序,当SpeechSynthesizer.SpeakTextAsync正在运行并从此处恢复时,我想暂停该应用程序。Windows Phone 8 SpeechSynthesizer暂停
await synthesizer.SpeakTextAsync(text);
终止阅读时var stop = true;
我正在开发一个应用程序,当SpeechSynthesizer.SpeakTextAsync正在运行并从此处恢复时,我想暂停该应用程序。Windows Phone 8 SpeechSynthesizer暂停
await synthesizer.SpeakTextAsync(text);
终止阅读时var stop = true;
有人在这里贴了一段时间后,在同一时间,我刷新页面,看他的回答,看到了一个通知&再次刷新页面&答案不见了。但是谁发布,他是一个拯救生命的人。它卡住了我的想法,我最终创造了这个。
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();
}
谢谢你,你的回答给了我这个主意。
那么,根据文档,当您拨打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()
。
它完成了!请享用...!
你打算怎么暂停?它会停止发言权吗? – Mani 2013-10-17 14:12:32