无法从UWP上的文本到语音音频创建URI文件
问题描述:
我试图创建带文本到语音通知的Toast通知作为音频通知。当用户输入“Let's eat”这样的描述并保存吐司时,当时间到了时,吐司会说“Let's eat”。它就像一个烤面包的铃声。无法从UWP上的文本到语音音频创建URI文件
我从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;
}
当我试图调试它,代码无法保存文本到语音转换结果存入档案。
此音频稍后将用作烤面包的自定义音频。
答
当我试图调试它时,代码无法将文本到语音结果保存到文件中。
我测试了你的代码在我身边。我得到了“流不支持写”的异常,所以问题非常明显。在你的代码中,你只需打开流以供阅读file.OpenStreamForReadAsync()
你应该使用file.OpenStreamForWriteAsync()
然后它会工作。
感谢您的回答。在更改为OpenStreamForWriteAsync()之后,它将尝试部分,但文本到语音仍然不出来。 –
@OliviaOlgaClarissa我已经与相关团队联系过。目前,它不支持临时文件夹。您可以将音频文件复制到本地文件夹。我们将考虑在未来版本的Windows中添加对临时文件夹的支持。 –
好的,谢谢你的信息,泽维尔 –