[英]Cannot create an URI file from text-to-speech audio on UWP
我试图用文本转语音作为音频通知来创建一个吐司通知。 当用户键入类似“ Let's eat”的描述并保存吐司时,时间到了,吐司将说“ Let's eat”。 就像烤面包的铃声一样。
我从Social 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;
}
当我尝试调试它时,代码无法将文本到语音的结果保存到文件中。
以后,此音频将用作吐司的自定义音频。
当我尝试调试它时,代码无法将文本到语音的结果保存到文件中。
我已经在您这边测试了您的代码。 我遇到了“流不支持写入”的异常,因此问题很明显。 在您的代码中,您只需打开流以读取文件file.OpenStreamForReadAsync()
您应该使用file.OpenStreamForWriteAsync()
然后它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.