playing a sound using html5/javascript

≡放荡痞女 提交于 2019-12-11 18:22:10

问题


My Problem: I would like a user to hear a sound of some sort if a number is over 100.

If (x > 100) { play sound }

How would I go about this using html5/javascript. Is it possible to play sound without having an inline little video clip. The only stuff I've seen thus far has those little quicktime players embedded within the page: http://www.w3schools.com/html/html_sounds.asp

Hopefully there's a better solution? Thanks


回答1:


Use an <audio> element.

var foo = document.createElement('audio');
foo.src = 'https://dl.dropbox.com/u/8090976/Matrix%20Ring.aiff';

if (x > 100)
{
    document.getElementById('foo').play();
}

Demo: http://jsfiddle.net/mattball/ZcRt9/

More reading: http://html5doctor.com/native-audio-in-the-browser



来源:https://stackoverflow.com/questions/12700008/playing-a-sound-using-html5-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!