FediMusicPlayer/audioPlayer.js
2023-04-23 00:17:41 -07:00

94 lines
2.6 KiB
JavaScript

/* FediMusicPlayer - An HTML5 + Javascript Music Player
* Copyright (C) 2023 <yanderefed@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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.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);
}