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

不打扰是莪最后的温柔 提交于 2019-12-02 09:54:32

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();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!