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

我可以将memorystream保存到文件流中,并且wav可以正常播放(在单独的函数中进行了测试),但是当尝试将memorystream发送到响应(下面的代码)时,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();

最佳答案

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

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

C# SpeechSynthesizer makes service unresponsive

10-02 04:12
查看更多