failbot8000
09-25-2012, 10:09 PM
Hello,
I am having trouble getting my audio to stop playing when I perform a second action.
Here is my html code:
<img src="picture1.png" style="cursor:pointer" onClick="chooseSound(0,0)">
<br /> <br /> <br />
<img src="picture2.png" style="cursor:pointer" onClick="chooseSound(0,1)">
And my javascript code is:
function chooseSound(first, second)
{
setTimeout("stopallSounds()",1);
for (var i=0; i < DemoSounds.length; i++)
{
if ( DemoSounds[i].index0==first && DemoSounds[i].index1==second )
{
playthisSound(DemoSounds[i].id);
}
}
}
function stopallSounds()
{
for (var i=0; i < DemoSounds.length; i++)
{
stopthisSound(DemoSounds[i].id)
}
}
function stopthisSound(someSound)
{
var mysound = document.getElementById(someSound);
mysound.currentTime = 0;
mysound.pause();
}
function playthisSound(someSound)
{
var mysound = document.getElementById(someSound);
mysound.load();
mysound.play();
}
The array in question is given by:
var DemoSounds =
[{ "index0" : "0" , "index1" : "0", "id" : "Audio00" },
{ "index0" : "0" , "index1" : "1", "id" : "Audio01" },
{ "index0" : "0" , "index1" : "2", "id" : "Audio02" },
{ "index0" : "0" , "index1" : "3", "id" : "Audio03" },
{ "index0" : "1" , "index1" : "0", "id" : "Audio10" },
{ "index0" : "1" , "index1" : "1", "id" : "Audio11" },
{ "index0" : "1" , "index1" : "2", "id" : "Audio12" },
{ "index0" : "1" , "index1" : "3", "id" : "Audio13" },];
All the IDs are defined in a separate file called audio html that I include using php. The problem is that while I can click on each image and play their respective audio, it doesn't necessarily stop the other audio from playing. Also, for some reason, I can't call stopallSounds() without putting it in a Timeout.
What am I doing wrong? How can I improve it? I don't want to use jQuery, or else I would have posted this question there!
Thanks everyone!
I am having trouble getting my audio to stop playing when I perform a second action.
Here is my html code:
<img src="picture1.png" style="cursor:pointer" onClick="chooseSound(0,0)">
<br /> <br /> <br />
<img src="picture2.png" style="cursor:pointer" onClick="chooseSound(0,1)">
And my javascript code is:
function chooseSound(first, second)
{
setTimeout("stopallSounds()",1);
for (var i=0; i < DemoSounds.length; i++)
{
if ( DemoSounds[i].index0==first && DemoSounds[i].index1==second )
{
playthisSound(DemoSounds[i].id);
}
}
}
function stopallSounds()
{
for (var i=0; i < DemoSounds.length; i++)
{
stopthisSound(DemoSounds[i].id)
}
}
function stopthisSound(someSound)
{
var mysound = document.getElementById(someSound);
mysound.currentTime = 0;
mysound.pause();
}
function playthisSound(someSound)
{
var mysound = document.getElementById(someSound);
mysound.load();
mysound.play();
}
The array in question is given by:
var DemoSounds =
[{ "index0" : "0" , "index1" : "0", "id" : "Audio00" },
{ "index0" : "0" , "index1" : "1", "id" : "Audio01" },
{ "index0" : "0" , "index1" : "2", "id" : "Audio02" },
{ "index0" : "0" , "index1" : "3", "id" : "Audio03" },
{ "index0" : "1" , "index1" : "0", "id" : "Audio10" },
{ "index0" : "1" , "index1" : "1", "id" : "Audio11" },
{ "index0" : "1" , "index1" : "2", "id" : "Audio12" },
{ "index0" : "1" , "index1" : "3", "id" : "Audio13" },];
All the IDs are defined in a separate file called audio html that I include using php. The problem is that while I can click on each image and play their respective audio, it doesn't necessarily stop the other audio from playing. Also, for some reason, I can't call stopallSounds() without putting it in a Timeout.
What am I doing wrong? How can I improve it? I don't want to use jQuery, or else I would have posted this question there!
Thanks everyone!