首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用嵌入式将文本转换为语音

使用嵌入式将文本转换为语音
EN

Stack Overflow用户
提问于 2013-04-23 06:19:41
回答 1查看 270关注 0票数 0

我有这个代码,它工作得很好,但我一直在努力让它输出一个可以嵌入到页面中的wav文件,而不是让用户下载和收听。任何帮助都是非常感谢的。我希望能够使用查询字符串直接调用该文件,以便它与captcha文本相匹配。Model.InstanceData代码是文本字符串中的验证码。我似乎就是不能对它动脑筋。谢谢!

代码语言:javascript
运行
复制
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);
}
EN

Stack Overflow用户

发布于 2013-04-24 04:36:04

基本上,经过多次尝试之后,解决方案比我想象的要简单一点。我在我的项目中创建了一个新的CaptchaPlayer.aspx播放器,并在后台的代码中使用了:

代码语言:javascript
运行
复制
 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);
        }
    }

}

之后,我可以将嵌入内容添加到表单页面中

代码语言:javascript
运行
复制
 <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>

这就成功了!我希望这对某些人有帮助。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16157702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档