java中的简单验证码音频设置

时间:2016-12-16 14:08:47

标签: java simplecaptcha

我有一个表单,我已经实现Simple captcha每件事情都运行正常,但我希望在用户点击播放按钮时播放音频验证码。在我的情况下,音频在页面加载时默认播放。

以下是我的音频验证码

public class MyAudioCaptcha extends AudioCaptchaServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    try {

        MyTextProducer myTextProducer  = (MyTextProducer) req.getSession().getAttribute("myTextProducer");
        if(myTextProducer == null){
            myTextProducer = new MyTextProducer();

        }
        AudioCaptcha ac = new AudioCaptcha.Builder().
                addAnswer(myTextProducer).
                addNoise().
                build(); // Required

                CaptchaServletUtil.writeAudio(resp, ac.getChallenge());
                req.getSession().setAttribute("audioCaptcha", ac);
    } catch (Exception e) {
        Util.AppendExceptionToLog(e);
    }

  }
}

JSP代码:

   <script>
        $(document).ready(function(){
            reloadCaptcha();
        });
      function reloadCaptcha(){
        var d = new Date();
        $.ajax({
          url:"captcha.jsp",
          type :"POST",
          async:true,
          data: { 
            'action':  'generateCaptcha' ,
          },
        success:function(response){
           $("#captcha_image").attr("src", "../../simpleCaptcha.png?"+d.getTime());
           $("#captcha_audio").attr("src", "../../audio.wav?"+d.getTime());

         },
     });    

    }
   </script>
<body>
    <td>
     <div class="inputfield" style="margin-left: 10px;">
        <img  id="captcha_image" /> 
        <img  src="../../images/reload.jpg" onclick="reloadCaptcha()" style="cursor: pointer;" alt="reload"width="30" height="30"/>
    </div>
   </td>
   <td>
        <audio controls autoplay  id="captcha_audio" autoplay = "false"></audio>
   </td>
</body>

在上面的代码中是否有任何可配置的音频不会在页面加载时播放,只有在用户点击音频验证码enter image description here时播放

1 个答案:

答案 0 :(得分:0)

您在autoplay标记中配置了autoplay = "faluse"<audio>,您应将其删除。

autoplay属性是一个布尔属性。

如果存在,音频将在不停止的情况下立即自动开始播放。

- From w3cschool