SpeechSynthesizer - How do I play/save the wav file?

流过昼夜 提交于 2019-12-20 05:39:53

问题


I have the following code snippet in an ASP.NET app (non Silverlight)

 string sText = "Test text";
 SpeechSynthesizer ss = new SpeechSynthesizer();
 MemoryStream ms = new MemoryStream();
 ss.SetOutputToWaveStream(ms);
 ss.Speak(sText);
 //Need to send the ms Memory stream to the user for listening/downloadin

How do I:

  1. Play this file on the browser

  2. Prompt for the user to download a wav file?

Can anyone help with completing the code?

EDIT: Any help is appreciated.


回答1:


Here's the main bit to an IHttpHandler that does what you want. Plug the handler URL into a bgsound tag or pipe it to whatever to play in-browser, and add a querystring check for a "downloadFile" var or something to conditionally add a Content-Disposition: attachment; filename=whatever.wav header if you want to download. No intermediate file is necessary (though there is weirdness with the SetOutputToWaveStream thing failing if it's not run on another thread).

    public void ProcessRequest(HttpContext context)
    { 
        MemoryStream ms = new MemoryStream();

        context.Response.ContentType = "application/wav";

        Thread t = new Thread(() =>
            {
                SpeechSynthesizer ss = new SpeechSynthesizer();
                ss.SetOutputToWaveStream(ms);
                ss.Speak("hi mom");
            });
        t.Start();

        t.Join();
        ms.Position = 0;
        ms.WriteTo(context.Response.OutputStream);
        context.Response.End();
    }


来源:https://stackoverflow.com/questions/1719780/speechsynthesizer-how-do-i-play-save-the-wav-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!