0

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
  • Please checkout musquitojs.com Commented Jun 25, 2018 at 15:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

so this will not create any inline clip, but will actually just allow me to play an audio snippet?
Edited to use document.createElement() instead of an existing one in the page. N.B. you do not need to create a new element every time you want to play a sound; you can re-use the same one.
great! so if I have the sound clip hosted on my server somewhere, I can just link to it using the whole URL??
jsfiddle doesnt seem to be working for me. It is not producing any sound on either Chrome or Firefox
My demo works for me in Safari on a Mac. It's possible that the codec isn't compatible with your OS/browser combination; try an mp3.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.