SpeechSynthesizer.SpeakTextAsyncの実行中に一時停止して、そこから再開するアプリを開発しています。
await synthesizer.SpeakTextAsync(text);
いつ読むのをやめるvar stop = true;
SpeechSynthesizer.SpeakTextAsyncの実行中に一時停止して、そこから再開するアプリを開発しています。
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()
ください。
終わった!楽しみ...!