HTML

HOW TO ADD BACKGROUND MUSIC IN HTML

INSERT AUDIO TAG SOMEWHERE AT BOTTOM OF THE PAGE <audio src="MUSIC.mp3" autoplay loop></audio>

SIMPLE BGM

01

HTML AUDIO TAG <audio src="1.mp3" autoplay id="bgm"></audio>

MULTIPLE SONGS

02

GET AUDIO TAG + DEFINE PLAYLIST  let aud = document.getElementById("bgm"),      playlist = ["1.mp3", "2.mp3"], now = 0;

AUTO LOAD NEXT SONG aud.onended = () => {   now++;   if (now>=playlist.length) { now = 0; }      aud.src = playlist[now];   aud.pause(); aud.load(); aud.play(); };

Audio will not always autoplay - Especially on mobile devices.

Sudden loud music will have a negative experience instead.

To give users the controls - <audio controls>

EXTRA NOTES

03