Aaron Aaron - 5 months ago 11
CSS Question

Audio Problems html5

I have encountered a slight issue on the website I am building. The problem is that the audio plays fine on the home page just like I want it to but when I navigate to other pages on my website then go back to the homepage, there seems to be TWO audio files playing. Here is my code so far for having the background audio on the home page.

<audio autoplay>
<source src="audio/sound.mp3" type = "audio/mpeg">
Your browser doesn't support audio
</audio>


Is it possible without the use of AJAX, or maybe even JavaScript to just make that audio restart on the home page with only one song playing instead of the same song playing twice? Any help or advice is greatly appreciated :)

Answer

Give this a shot! It just stops, resets, and plays the audio when you land on the page. Make sure to give the audio file a corresponding ID.

   var mySong = document.getElementById("mySong");
   mySong.pause();
   mySong.currentTime = 0;
   mySong.play();
Comments