本文介绍了SpeechSynthesizer - 如何播放/保存为wav文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET应用程序下面的code段(非Silverlight的)

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

我如何:

  1. 播放浏览器上的文件

  1. Play this file on the browser

提示用户下载一个wav文件?

Prompt for the user to download a wav file?

任何人都可以帮助完成code?

Can anyone help with completing the code?

编辑:任何帮助是AP preciated

推荐答案

下面是主要的位到IHttpHandler的,做你想要的。插上处理URL转换为BGSOUND标签或管到什么在浏览器中播放,并添加查询字符串检查了downloadFileVAR或东西有条件地添加内容处置:附件;如果你想下载的文件名= whatever.wav头。没有中间文件是必要的(虽然有古怪与SetOutputToWaveStream事情失败,如果它不能在另一个线程上运行)。

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

这篇关于SpeechSynthesizer - 如何播放/保存为wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:46