使用嵌入的文本到语音

时间:2013-04-22 22:19:41

标签: c# asp.net .net text-to-speech

我有这个代码,它工作正常,但我一直在努力如何让它输出一个可以嵌入页面的wav文件,而不是让用户下载和收听。 任何帮助是极大的赞赏。我希望能够使用查询字符串直接调用该文件,以便它与captcha文本匹配。 Model.InstanceData [“Code”]是文本字符串中的验证码。我似乎无法绕过它。谢谢!

private void _playBtn_Click(object sender, ImageClickEventArgs e)
{
    HttpContext context = HttpContext.Current;

    if (context == null || context.Response == null)
    {
        return;
    }

    context.Response.Clear();

    context.Response.AddHeader("content-disposition", "attachment; filename=" + "captcha.wav");
    context.Response.AddHeader("content-transfer-encoding", "binary");

    context.Response.ContentType = "audio/wav";

    SoundGenerator soundGenerator = new SoundGenerator(Model.InstanceData["Code"]);

    MemoryStream sound = new MemoryStream();

    // Write the sound to the response stream 
    soundGenerator.Sound.Save(sound, SoundFormatEnum.Wav);

    sound.WriteTo(context.Response.OutputStream);
}

1 个答案:

答案 0 :(得分:0)

基本上在解决方案之后,比我想象的要简单一些。我在我的项目中创建了一个新的CaptchaPlayer.aspx播放器,后面的代码使用了这个:

 protected void Page_Load(object sender, EventArgs e)
    {
        PlayCaptchaSession();
    }

    private void PlayCaptchaSession()
    {
        object o = Session["captcha"];

        if (o != null)
        {
            HttpContext context = HttpContext.Current;

            if (context == null || context.Response == null)
            {
                return;
            }

            //context.Response.AddHeader("content-disposition", "inline; filename=" + "captcha.wav");
            context.Response.AddHeader("content-transfer-encoding", "binary");
            context.Response.ContentType = "audio/wav";
            SoundGenerator soundGenerator = new SoundGenerator(o as string);
            MemoryStream sound = new MemoryStream();
            // Write the sound to the response stream 
            soundGenerator.Sound.Save(sound, SoundFormatEnum.Wav);
            sound.WriteTo(context.Response.OutputStream);
        }
    }

}

之后我能够将嵌入添加到表单页面

 <embed id='captchaAudio' width='1' height='1' enablejavascript='true' autostart='false' name='Verification code player' src='/CaptchaPlayer.aspx'>
                <noembed><a href='/CaptchaPlayer.aspx' title='Verification code wave audio file download'>Audio File</a></noembed>
                 <script type="text/javascript">
                 $(document).ready(function(){
                     $(".capLink").click(function () {
                         //alert(".click() called.");
                         document.getElementById('captchaAudio').Play();

                     });
                 });    
                </script>

这就是诀窍!我希望这有助于某人。