Im trying to build a simple audio player with the HTML5 audio tag. I have 3 <source> elements nested within the main <audio> element, each pointing to an audio track with multiple formats for cross-browser support. I am using a simple button to trigger a "click" event with the following code:
<audio id="myAudio" preload="none">
<source src="audio/demo.m4a" type="audio/m4a" />
<source src="audio/demo.mp3" type="audio/mp3" />
<source src="audio/demo.ogg" type="audio/ogg" />
<!-- other sources go here -->
</audio>
var audio = document.getElementById("myAudio");
// Change track!
$("#mute").on("click", function(evt) {
$("#myAudio > source:gt(2)").attr({ src: "audio/demo2.ogg" , type: "audio/ogg"});
if(!audio.paused) {
audio.pause();
audio.play();
}
});
As you may guess, I'm trying to change the third elements src attribute by applying a :gt(2) filter. Not only am I trying to increment the track, I am also trying to stop the current track from playing so the new track can start playing immediately when the button is pressed. So far this is not having any effect on my webpage.
audio.load()after you switch sources