我正在尝试使用文字转语音作为音频通知创建一个Toast通知。当用户输入“Let's eat”这样的描述并保存吐司时,吐司时会说“让我们吃”。它像吐司的铃声。
我从Social MSDN得到了如何从文本到语音创建Toast通知的答案,但在我的程序中它总是转向异常。
这是用于创建文字转语吐司通知的代码:
private static async Task<Uri> AudioforToast(string text)
{
var voices = SpeechSynthesizer.AllVoices;
using (var synthesizer = new SpeechSynthesizer())
{
if (!String.IsNullOrEmpty(text))
{
try
{
synthesizer.Voice = voices.First(gender => gender.Gender == VoiceGender.Female);
// Create a stream from the text.
SpeechSynthesisStream synthesisStream = await synthesizer.SynthesizeTextToStreamAsync(text);
// And now write that to a file
var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("ToastAudio", CreationCollisionOption.ReplaceExisting);
using (var fileStream = await file.OpenStreamForReadAsync())
{
await synthesisStream.AsStream().CopyToAsync(fileStream);
}
// And then return the file path
return new Uri("ms-appdata:///temp/ToastAudio");
}
catch (Exception)
{
// If the text is unable to be synthesized, throw an error message to the user.
var messageDialog = new Windows.UI.Popups.MessageDialog("Unable to synthesize text. Toast Audio is default");
await messageDialog.ShowAsync();
}
}
}
// If the text is unable to be synthesized, don't return a custom sound
return null;
}
当我尝试调试它时,代码无法将文本到语音结果保存到文件中。
此音频稍后将用作吐司的自定义音频。
答案 0 :(得分:0)
当我尝试调试它时,代码无法将文本到语音结果保存到文件中。
我已经在我身边测试了你的代码。我得到了例外&#34;流不支持写&#34;,所以问题非常明显。在您的代码中,您只需打开流阅读file.OpenStreamForReadAsync()
您应该使用file.OpenStreamForWriteAsync()
然后它会起作用。