Javascript: Multiple Sounds, Stop Sounds Before Playing Another

Was trying to build a little script that has embedded sounds that play from a button onClick.  Sticking it online because as I searched for an answer for this, I found nothing that solved it so simply.

Came up with:

<script>
function EvalSound(soundobj)
{
var stopsound1=document.getElementById(‘sound1’);
stopsound1.Stop();
var stopsound2=document.getElementById(‘sound2’);
stopsound2.Stop();
var stopsound3=document.getElementById(‘sound3’);
stopsound3.Stop();
var thissound=document.getElementById(soundobj);
thissound.Rewind();
thissound.Play();
}
</script>

<embed src=”html/media/wood-logs-1.mp3″ autostart=false width=1 height=1 id=”sound1″ enablejavascript=”true”>

<embed src=”html/media/water-dripping-1.mp3″ autostart=false width=1 height=1 id=”sound2″ enablejavascript=”true”>

<embed src=”html/media/fire-1.mp3″ autostart=false width=1 height=1 id=”sound3″ enablejavascript=”true”>

<title>Stop and Play Sounds</title> </head>

<body>
<form> <input type=”button” value=”1″ onClick=”EvalSound(‘sound1’)”> </form>

<form> <input type=”button” value=”2″ onClick=”EvalSound(‘sound2’)”> </form>

<form> <input type=”button” value=”3″ onClick=”EvalSound(‘sound3’)”> </form>

</body>


Leave a Reply