【问题标题】:How to write audio stream out to response using SpeechSynthesizer C#如何使用 SpeechSynthesizer C# 将音频流写入响应
【发布时间】:2014-03-05 01:54:51
【问题描述】:

我在 C# asp.net 应用程序中使用 SpeechSynthesizer 类尝试将一些文本转换为语音,然后将该音频内存流写入响应,以便最终用户可以将其下载为 .wav。

我可以将内存流保存到文件流并且 wav 可以正常播放(在单独的函数中对此进行了测试)但是当尝试将内存流发送到响应(下面的代码)时,看起来 IE 说正在下载 .wav 文件然而,它几乎就像是有东西挂在对象上,不会让它完成下载。结果.wav 无法打开播放声音。

关于如何正确执行此操作的任何想法?下面是我的代码。

        SpeechSynthesizer synth = new SpeechSynthesizer();
        MemoryStream streamAudio = new MemoryStream();

        // Configure the synthesizer to output to an audio stream.
        synth.SetOutputToWaveStream(streamAudio);

        // Speak a phrase.
        synth.Speak("This is sample text-to-speech output.");

        // Set audio stream to beginning
        streamAudio.Position = 0;

        // Send the memory steam bytes out to the response/
        Response.Clear();
        Response.ContentType = "audio/wav";
        Response.AppendHeader("Content-Disposition", "attachment; filename=mergedoutput.wav");
        streamAudio.CopyTo(Response.OutputStream);
        Response.Flush();
        Response.End();

【问题讨论】:

    标签: c# audio text-to-speech


    【解决方案1】:

    找到了解决办法... speak 方法需要在自己的线程中运行,然后返回可以被写入响应的字节数组。

    在这篇文章中找到了解决方案

    C# SpeechSynthesizer makes service unresponsive

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2016-03-13
      相关资源
      最近更新 更多