首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在UWP上从文本到语音音频创建URI文件。

问无法在UWP上从文本到语音音频创建URI文件。
EN

Stack Overflow用户
提问于 2016-12-14 02:42:33
回答 1查看 141关注 0票数 0

我试图创建一个吐司通知,文本到语音作为音频通知。当用户输入诸如“让我们吃”和保存吐司之类的描述时,当时机成熟时,祝酒词会说“让我们吃”。就像烤面包的铃声。

我从社会MSDN那里得到了如何从文本到语音创建祝酒词通知的答案,但是在我的程序中,它总是变成异常。

这是创建文本到语音吐司通知的代码:

代码语言:javascript
复制
 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;
    }

当我尝试调试它时,代码无法将文本到语音结果保存到文件中.

此音频稍后将用作烤面包的自定义音频。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-15 07:16:06

当我尝试调试它时,代码无法将文本到语音结果保存到文件中.

我在我身边测试过你的代码。我得到了一个例外,“流不支持写”,所以这个问题非常明显。在您的代码中,您只需打开读取file.OpenStreamForReadAsync()的流,就应该使用file.OpenStreamForWriteAsync(),然后它就能工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41133977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档