I have the following as part of my html (once for each of several songs):
<audio
name="aMedia"
id="aMedia"
controls
preload="auto">
<source src="audio/mysong.m4a">
</audio>
I wish to determine if the above standard Play button was pressed to play a song. If it was, I need to first stop all other songs that may be playing, e.g.,
function stopAllSongs()
{
var allMedia = document.getElementsByName(theMedia), thisMedia;
for (m=0; m < allMedia.length; m++)
{
thisMedia = allMedia[m];
if (thisMedia.isPlaying())
thisMedia.pause();
}
}
I have seen successful uses of <input ...>
to do what I wish; but I would like to avoid that if possible.
Any ideas?
You could use the
play
event for this, then iterate through all other audio elements and invoke pause on them.You can use a common handler for all the audio elements, the
this
object will represent the actual audio element triggering the event (which in turn of course can be used to set a CSS class etc.).pause()
can be called even if the audio is already paused so we don't need to do additional checks for it. We just loop through the array of elements and skip the element that triggered the event.Example