0

I have a mobile website that plays a background song. When I pressed the home button on my phone and leave the browser the song keeps playing until I go back on the browser and close the tab. this is the code for the song I have.

<audio preload="auto" src="song.mp3" loop="true" autobuffer id="myAudio">
Unsupported in Firefox
</audio>

How would I make it so the song only plays when I am viewing the page? Browser is Google Chrome and device is Android.

Russ J
  • 828
  • 5
  • 12
  • 24

1 Answers1

0

You could add an if condition in your javascript code which checks if your tab with the webpage is active and then play the music, else pause it check out

this stackoverflow question for checking if your tab is active

RishiC
  • 768
  • 1
  • 10
  • 34