我试图创建一个吐司通知,文本到语音作为音频通知。当用户输入诸如“让我们吃”和保存吐司之类的描述时,当时机成熟时,祝酒词会说“让我们吃”。就像烤面包的铃声。
我从社会MSDN那里得到了如何从文本到语音创建祝酒词通知的答案,但是在我的程序中,它总是变成异常。
这是创建文本到语音吐司通知的代码:
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;
}当我尝试调试它时,代码无法将文本到语音结果保存到文件中.
此音频稍后将用作烤面包的自定义音频。
发布于 2016-12-15 07:16:06
当我尝试调试它时,代码无法将文本到语音结果保存到文件中.
我在我身边测试过你的代码。我得到了一个例外,“流不支持写”,所以这个问题非常明显。在您的代码中,您只需打开读取file.OpenStreamForReadAsync()的流,就应该使用file.OpenStreamForWriteAsync(),然后它就能工作了。
https://stackoverflow.com/questions/41133977
复制相似问题