Here's the main bit for IHttpHandler, which does what you want. Connect the handler url to the bgsound tag or connect it to what you want to play in the browser and add a querystring check for the "downloadFile" var or something that conditionally adds Content-Disposition: attachment; filename = whatever.wav if you want to download. An intermediate file is not required (although there is a strange thing when the SetOutputToWaveStream object does not work if it does not start 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(); }
source share