简体   繁体   English

无法通过UWP上的文本到语音音频创建URI文件

[英]Cannot create an URI file from text-to-speech audio on UWP

I was trying to create a toast notification with text-to-speech as the audio notification. 我试图用文本转语音作为音频通知来创建一个吐司通知。 When user type a description like "Let's eat" and save the toast, when the time is come the toast will say "Let's eat". 当用户键入类似“ Let's eat”的描述并保存吐司时,时间到了,吐司将说“ Let's eat”。 It like an ringtone for toast. 就像烤面包的铃声一样。

I got the answer from Social MSDN how to create an toast notification from text-to-speech, but at my program it always turn to exception. 我从Social MSDN获得了有关如何从文本到语音创建敬酒通知的答案,但是在我的程序中,它总是变成异常。

This is the code for creating text-to-speech toast notification: 这是用于创建文本到语音吐司通知的代码:

 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;
    }

When I tried to debug it, the code cannot save the text-to-speech result into file. 当我尝试调试它时,代码无法将文本到语音的结果保存到文件中。

This audio later will be use as a custom audio for toast. 以后,此音频将用作吐司的自定义音频。

When I tried to debug it, the code cannot save the text-to-speech result into file. 当我尝试调试它时,代码无法将文本到语音的结果保存到文件中。

I've tested your code in my side. 我已经在您这边测试了您的代码。 I got the exception "the stream doesn't support write", so the question was very obvious. 我遇到了“流不支持写入”的异常,因此问题很明显。 In your code, you just open stream for read file.OpenStreamForReadAsync() You should use file.OpenStreamForWriteAsync() Then it will work. 在您的代码中,您只需打开流以读取文件file.OpenStreamForReadAsync()您应该使用file.OpenStreamForWriteAsync()然后它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM