FediMusicPlayer/audioPlayer.js

79 lines
1.9 KiB
JavaScript

function appendMusicList(el, title, time)
{
const tr = document.createElement("tr")
const tdtitle = document.createElement("td")
const tdtime = document.createElement("td")
const div = document.createElement("div")
tr.setAttribute("id", "queue-item")
tdtime.setAttribute("width", "50")
tdtime.innerHTML = time
div.setAttribute("class", "ellipsis")
div.setAttribute("colspan", "2")
div.innerHTML = title
tdtitle.appendChild(div)
tr.appendChild(tdtitle)
tr.appendChild(tdtime)
el.appendChild(tr)
return tr
}
// https://github.com/semibran/wrap-around/blob/master/index.js
function wrap(idx, len)
{
return idx >= 0 ? idx % len : (idx % len + len) % len
}
function audioPlayer()
{
const aPlay = document.getElementById("audioPlayer")
const aList = document.getElementById("audioPlaylist")
const aTrack = document.getElementById("audioTrack")
const aArtist = document.getElementById("audioArtist")
const aCoverArt = document.getElementById("audioCoverArt")
var currentSong = null
var aListLinks = []
function changeSong(idx, doplay=true)
{
if (idx === currentSong)
{
aPlay.play()
return
}
if (currentSong !== null)
{
aListLinks[currentSong].classList.remove("current-song");
}
currentSong = wrap(idx, aListLinks.length)
aListLinks[currentSong].classList.add("current-song");
const aSelected = aListLinks[currentSong].getElementsByTagName("div")[0]
aPlay.src = musicList[currentSong][0]
aTrack.innerHTML = musicList[currentSong][1]
aArtist.innerHTML = musicList[currentSong][2]
aCoverArt.src = musicList[currentSong][4]
if (doplay === true)
{
aPlay.play()
}
}
musicList.forEach((value, index, array) => {
const el = appendMusicList(aList, value[1], value[3])
aListLinks.push(el)
el.addEventListener("click", () => {
// el.preventDefault();
changeSong(index)
})
})
aPlay.addEventListener("ended", function(){
changeSong(currentSong + 1)
})
changeSong(0, false);
}