SpeechSynthesizer - How to play / save wav file?

I have the following code snippet in an ASP.NET application (not 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 

Like me:

  • Play this file in a browser

  • Request to download wav file by user?

Can someone help with filling out the code?

EDIT: Any help is appreciated.

0
source share
1 answer

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(); } 
+2
source

Source: https://habr.com/ru/post/1433262/


All Articles