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

后端 未结 1 570
别跟我提以往
别跟我提以往 2021-01-27 15:45

I have the following code snippet in an ASP.NET app (non Silverlight)

 string sText = \"Test text\";
 SpeechSynthesizer ss = new SpeechSynthesizer();
 MemoryStre         


        
相关标签:
1条回答
  • 2021-01-27 16:11

    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 讨论(0)
提交回复
热议问题