FediMusicPlayer/audioPlayer.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-04-23 00:17:41 -07:00
/* FediMusicPlayer - An HTML5 + Javascript Music Player
2023-04-24 19:58:30 -07:00
* Copyright (C) 2023 <@Anon@yandere.cc>
2023-04-23 00:17:41 -07:00
*
* This program is free software: you can redistribute it and/or modify
2023-04-23 00:29:17 -07:00
* it under the terms of the GNU Affero General Public License as published by
2023-04-23 00:17:41 -07:00
* 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
2023-04-23 00:29:17 -07:00
* GNU Affero General Public License for more details.
2023-04-23 00:17:41 -07:00
*
2023-04-23 00:29:17 -07:00
* You should have received a copy of the GNU Affero General Public License
2023-04-23 00:17:41 -07:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2023-04-17 20:54:28 -07:00
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")
2023-04-17 20:54:28 -07:00
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
}
2023-04-17 20:54:28 -07:00
function audioPlayer()
{
const aPlay = document.getElementById("audioPlayer")
const aList = document.getElementById("audioPlaylist")
const aTrack = document.getElementById("audioTrack")
const aArtist = document.getElementById("audioArtist")
2023-04-17 20:54:28 -07:00
const aCoverArt = document.getElementById("audioCoverArt")
var currentSong = null
var aListLinks = []
2023-04-17 20:54:28 -07:00
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)
2023-04-17 20:54:28 -07:00
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]
2023-04-17 20:54:28 -07:00
if (doplay === true)
{
2023-04-19 22:35:35 -07:00
aPlay.play()
2023-04-17 20:54:28 -07:00
}
}
musicList.forEach((value, index, array) => {
const el = appendMusicList(aList, value[1], value[3])
aListLinks.push(el)
2023-04-17 20:54:28 -07:00
el.addEventListener("click", () => {
// el.preventDefault();
2023-04-19 22:35:35 -07:00
changeSong(index)
})
2023-04-17 20:54:28 -07:00
})
aPlay.addEventListener("ended", function(){
2023-04-19 22:35:35 -07:00
changeSong(currentSong + 1)
2023-04-17 20:54:28 -07:00
})
changeSong(0, false);
}