вам нужно сначала собрать все музыкальные файлы в массиве музыкальных файлов, а затем начать управлять всеми ними
пример:
// array who has all musics as playlist
var Musics = [] ;
// for target current played music in playlist Musics
var currentMusicPlayed = 0;
// 3 paths of music
let arrayOfPaths = ["./Music1.mp3" , "./Music2.mp3" , "./Music3.mp3"];
// loop for making audio objects and push all of them in Musics Playlist array
for(let i = 0 ; i < arrayOfPaths.length ; i += 1){
let music = new Audio();
music.src = arrayOfPaths[i];
Musics.push(music);
}
// music player
function MusicListPlayer(){
// play current music who has
Musics[currentMusicPlayed].play();
// and stop any music
for(let i = 0 ; i < Musics.length ; i += 1){
if(i != currentMusicPlayed){
Musics[currentMusicPlayed].pause();
}
}
// if current music index >= must be start from 0 again
if(currentMusicPlayed >= i ) currentMusicPlayed = 0;
// else increment for next click
else currentMusicPlayed += 1;
}
// just a random element for example
const buttonGetNextMusic = document.querySelector("any");
// here in click MusicListPlayer must be called and make next music in Array work
buttonGetNextMusic.addEventListener("click" , MusicListPlayer);
Надеюсь, этот пример поможет вам понять идею