How to play sounds in JavaScript

I have the following:

<audio id="clickdown-wav" src="ClickDown.wav" preload="auto"></audio>

and then in my JavaScript, I have:

var ClickDown = $('#clickdown-wav')[0];

$(document).delegate('a','click',function() {
    ClickDown.play();
});

The problem is that it's not too snappy (on an iPad) as I press anchor tags. There is a noticeable lag between the time I press the anchor tag and the wav file plays.

Q: Is there a source code only solution to playing sounds from JavaScript? Clickdown.wav is only 1k.


On iOS, there is no preloading. This was done by Apple on purpose, so that the user wouldn't have to use unnecessary bandwidth.


You can try to place an invisible "button" over your web app, which get's click with the first user interaction. After that interaction sound.play(); should work. Look at this thread.


您需要使用触摸事件而不是点击事件,将点击切换到“touchstart”,延迟将消失

链接地址: http://www.djcxy.com/p/57792.html

上一篇: 如何在config / initializers运行后设置Action Mailer默认值?

下一篇: 如何在JavaScript中播放声音