Apollo
Apollo

Reputation: 9064

playing a sound using html5/javascript

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

Upvotes: 0

Views: 1413

Answers (1)

Matt Ball
Matt Ball

Reputation: 360026

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

Upvotes: 2

Related Questions