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

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();
 //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 is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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.Speak("hi mom");

        ms.Position = 0;

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply