Im trying to create a simple countdown timer that plays a beep when it's done. My countdown timer is working. My sound file has been created and is in place. The script to play the sound file is not working, and thats what Im asking for help with.
Ive looked around for answers, and most recommend hooking up to an external JS library, which I do not want to do. That being said, here is my code:
<script language="Javascript">
var countdown;
var countdown_number;
var audio = new Audio('audio/beep.mp3');
function countdown_init() {
countdown_number = 11;
countdown_trigger();
}
function countdown_trigger() {
if(countdown_number > 0) {
countdown_number--;
document.getElementById('countdown_text').innerHTML = countdown_number;
if(countdown_number > 0) {
countdown = setTimeout('countdown_trigger()', 1000);
if(countdown_number === 0) { audio.play(); }
}
}
}
function countdown_clear() {
clearTimeout(countdown);
}
</script>
<div>
<input type="button" value="start countdown" onClick="countdown_init()" />
<input type="button" value="stop countdown" onClick="countdown_clear()" /> <br /><br />
</div>
<div id="countdown_text">Placeholding text</div>
The issue with your sound manager was that you needed to call createSound after the onready
event:
var countdown;
var countdown_number;
var mySoundObject;
function countdown_trigger() {
if (countdown_number > 0) {
countdown_number--;
document.getElementById('countdown_text').innerHTML = countdown_number;
if (countdown_number > 0) {
countdown = setTimeout(countdown_trigger, 1000);
}
if (countdown_number === 0) {
mySoundObject.play();
}
}
}
function countdown_clear() {
clearTimeout(countdown);
}
function countdown_init() {
countdown_number = 11;
countdown_trigger();
}
document.getElementById('start').onclick = countdown_init;
document.getElementById('stop').onclick = countdown_clear;
soundManager.setup({
url: 'http://ivdemo.chaseits.co.uk/SoundManager2-2.97a.20131201/swf/soundmanager2_flash_xdomain/soundmanager2_flash9_debug.swf',
flashVersion: 9,
useHTML5Audio: true,
html5Test: 'maybe',
preferFlash: false,
onready: function () {
mySoundObject = soundManager.createSound({
id: 'mySound',
url: 'http://www.freshly-ground.com/misc/music/20060826%20-%20Armstrong.mp3'
});
}
});
JSFiddle Demo http://jsfiddle.net/TBS8C/16/
EDIT
It looks like you changed your question to not using soundManager anymore. Here's a demo with HTML5 audio only.
var countdown;
var countdown_number;
var audio = new Audio('http://www.freshly-ground.com/misc/music/20060826%20-%20Armstrong.mp3');
function countdown_trigger() {
if (countdown_number > 0) {
countdown_number--;
document.getElementById('countdown_text').innerHTML = countdown_number;
if (countdown_number > 0) {
countdown = setTimeout(countdown_trigger, 1000);
}
if (countdown_number === 0) {
audio.play()
}
}
}
function countdown_clear() {
clearTimeout(countdown);
}
function countdown_init() {
countdown_number = 11;
countdown_trigger();
}
document.getElementById('start').onclick = countdown_init;
document.getElementById('stop').onclick = countdown_clear;
<input type="button" value="start countdown" id="start" />
<input type="button" value="stop countdown" id="stop" />
<div id="countdown_text"></div>
JSFiddle Demo http://jsfiddle.net/nsmp8mfv/1/