views:

390

answers:

1

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.

A: 

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();
    }
nitzmahone
For 1) I tried ms.WriteTo(Context.Response.OutputStream); ms.Close();with no luck. Am I missing something.2) The code does not produce a wav file as far as I can tell. How do I let the user download a wav/mp3 file without having to save it in a temp location on the server?
That process works for me- generates a wav stream of the spoken text. I don't know the context of the browser stuff you're doing- you can't render HTML in the same response or anything. You'd need to use an embed tag and pass parameters to a handler to generate the wav output or something...
nitzmahone
What I am trying to do is synthesize text to speech and give the user an ability to either (1) listen to it on the browser or (2) download the file. For (1), I understand I need the embed tag, but do I need to save a temp wav file that the embed tag points to? For (2), I set the contenttype, but it doesn't seem to be working. Could you please post a code snippet? Thanks!
When I add your code to a WebMethod, I get the "Timeout not supported" exception. Any clue?
The actual exception is System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Timeouts are not supported on this stream. at System.IO.Stream.get_ReadTimeout()Please help. I am stuck!
Hmph- nice bug on their part (it should ignore a failure on ReadTimeout). Just write a wrapper class that responds to ReadTimeout (just return whatever). Just make a class that extends Stream and delegates to another stream for everything but ReadTimeout.
nitzmahone