/* FediMusicPlayer - An HTML5 + Javascript Music Player * Copyright (C) 2023 Anon <@Anon@yandere.cc> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ 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") 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 } 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.load() aPlay.play() return } if (currentSong !== null) { aListLinks[currentSong].classList.remove("current-song"); } currentSong = wrap(idx, aListLinks.length) aListLinks[currentSong].classList.add("current-song"); aPlay.querySelectorAll("source").forEach(source => source.remove()) const sources = musicList[currentSong][3] sources.forEach(source => { const newSource = document.createElement("source") newSource.src = source.src newSource.type = source.type aPlay.appendChild(newSource) }) // aPlay.src = musicList[currentSong][3] aTrack.innerHTML = musicList[currentSong][0] aArtist.innerHTML = musicList[currentSong][1] aCoverArt.src = musicList[currentSong][4] if (doplay === true) { aPlay.load() aPlay.play() } } musicList.forEach((value, index, array) => { const trackTitle = value[0] const trackTime = value[2] const el = appendMusicList(aList, trackTitle, trackTime) aListLinks.push(el) el.addEventListener("click", () => { // el.preventDefault(); changeSong(index) }) }) aPlay.addEventListener("ended", function(){ changeSong(currentSong + 1) }) changeSong(0, false); }