开发者

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

开发者 https://www.devze.com 2022-12-11 18:40 出处:网络
I have the following code snippet in an ASP.NET 开发者_StackOverflow社区app (non Silverlight) string sText = \"Test text\";

I have the following code snippet in an ASP.NET 开发者_StackOverflow社区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.


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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号