Как я могу переписать этот код, чтобы одновременно воспроизводить только одну песню?
Нужно чтоб нажав на песню выключить предыдущее и воспроизводить следующее как в обычном аудиоплеере.
function Music(name, author, source, cover) {
this.name = name;
this.author = author;
this.source = source;
this.cover = cover;
}
let music1 = new Music('Believer', 'Imagine Dragons', 'music/Imagine Dragons - Believer.mp3', 'img/first.jpg');
let music2 = new Music('Natural', 'Imagine Dragons', 'music/Imagine Dragons - Natural.mp3', 'img/second.jpg');
let music3 = new Music('Thunder', 'Imagine Dragons', 'music/Imagine Dragons - Thunder.mp3', 'img/third.jpg');
let music4 = new Music('Lovers in Japan', 'Coldplay', 'music/Coldplay - Lovers in Japan .mp3', 'img/coldplay1.jpg');
let music5 = new Music('The Scientist', 'Coldplay', 'music/Coldplay - The Scientist.mp3', 'img/coldplay2.jpg');
let music6 = new Music('Lovers in Japan', 'Coldplay', 'music/Coldplay - Adventure Of A Lifetime.mp3', 'img/coldplay3.jpg');
let musicArr = [music1, music2, music3,music4, music5, music6];
let playlist = document.querySelector('#playList');
let player = document.querySelector('#player');
let playlistName = document.querySelector('#playlistName');
let playlistAuthor = document.querySelector('#playlistAuthor');
let imgSrc = document.querySelector('#imgSrc');
musicArr.forEach((mp3,index) => {
let li = document.createElement('li');
li.classList.add('d-flex', 'align-items-start')
playlist.appendChild(li);
let musicName = mp3.name;
let authorName = mp3.author;
let musicSrc = mp3.source;
let coverSrc = mp3.cover;
let litleImg = document.createElement('img');
litleImg.setAttribute('src', coverSrc);
litleImg.style.width = '20%';
li.appendChild(litleImg);
let titleDiv = document.createElement('div');
li.appendChild(titleDiv);
titleDiv.classList.add('ml-2');
let pName = document.createElement('p');
pName.innerHTML = musicName;
titleDiv.appendChild(pName);
let pAuthor = document.createElement('p');
pAuthor.innerHTML = authorName;
titleDiv.appendChild(pAuthor);
li.addEventListener('click', (e) => {
playlistName.innerHTML = musicName;
playlistAuthor.innerHTML = authorName;
imgSrc.setAttribute('src', coverSrc);
let currentSong = musicArr[index];
let audio = new Audio(currentSong.source);
audio.play()