Skip to content

Commit 06b875f

Browse files
authored
update v1.4 18/04/2025
added audioplayer list play
1 parent 5342d29 commit 06b875f

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

templates/footer.html

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
filterelement = "genre";
5555
}
5656
if (window.localStorage.getItem('tdelement') === '5') {
57+
filterelement = "theme";
58+
}
59+
if (window.localStorage.getItem('tdelement') === '6') {
5760
filterelement = "year";
5861
}
5962
if (window.localStorage.getItem('tdelement') === null) {
@@ -67,8 +70,62 @@
6770
}
6871
document.getElementById("searchbox").innerHTML = text;
6972
document.getElementById("srinput").focus();
70-
//window.alert(text);
7173

74+
// audio player routines todo needs optimization and further tweaks
75+
const trElements = document.querySelectorAll('tr.trlight');
76+
const audio = document.getElementById('audio');
77+
let currentIndex = 0;
78+
let urls = [];
79+
let titles = [];
80+
let shuffle = false;
81+
audio.volume = 0.5;
82+
83+
trElements.forEach(trElement => {
84+
const onclickAttribute = trElement.getAttribute('onclick');
85+
const urlMatch = onclickAttribute.match(/audioplay\('([^']+)'/);
86+
if (urlMatch && urlMatch[1]) {
87+
urls.push(urlMatch[1]);
88+
const titleElement = trElement.querySelector('td:nth-child(3)');
89+
titles.push(titleElement ? titleElement.textContent : 'Unknown Title');
90+
}
91+
});
92+
93+
let previousPicks = [];
94+
95+
function shuffleplay(set) {
96+
if (previousPicks.length === set) {
97+
// reset picks
98+
previousPicks = [];
99+
}
100+
101+
let shuffleindex;
102+
do {
103+
shuffleindex = Math.floor(Math.random() * set);
104+
} while (previousPicks.includes(shuffleindex));
105+
// ensure the pick is not a previous pick
106+
107+
previousPicks.push(shuffleindex);
108+
console.log('pick: ', shuffleindex);
109+
return shuffleindex;
110+
}
111+
112+
// Accessing previous picks
113+
//let mostRecentPick = previousPicks[currentIndex - 1]; // 5
114+
//let secondMostRecentPick = previousPicks[currentIndex - 2]; // 1
115+
//let thirdMostRecentPick = previousPicks[currentIndex - 3]; // 7
116+
117+
118+
audio.addEventListener('ended', () => {
119+
//console.log('Playback ended!');
120+
if (currentIndex == trElements.length) {
121+
currentIndex = 0;
122+
}
123+
// shuffle play
124+
if (shuffle) {
125+
currentIndex = shuffleplay(trElements.length);
126+
}
127+
audioplay(urls[currentIndex], trElements[currentIndex]);
128+
});
72129
</script>
73130
</body>
74131
</html>

templates/lib.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,60 @@ function showDivs(n) {
9393

9494
document.onkeydown = function (event) {
9595
var kbpressed = event.key;
96+
// tricky overrides browser shortcut keys
97+
//event.preventDefault();
98+
// todo harmonize with audio end
99+
if (currentIndex == trElements.length) {
100+
currentIndex = 0;
101+
}
102+
// audio previous
96103
if(kbpressed == "ArrowLeft") {
97104
plusDivs(-1);
105+
// shuffle play
106+
//if (shuffle) {
107+
// currentIndex = previousPicks[currentIndex - 1];
108+
// audioplay(urls[currentIndex], trElements[currentIndex]);
109+
//} else {
110+
if (currentIndex > 1) {
111+
audioplay(urls[currentIndex - 2], trElements[currentIndex - 2]);
112+
}
113+
//}
98114
}
115+
// audio next
99116
if(kbpressed == "ArrowRight") {
100117
plusDivs(1);
118+
// shuffle play
119+
if (shuffle) {
120+
currentIndex = shuffleplay(trElements.length);
121+
}
122+
audioplay(urls[currentIndex], trElements[currentIndex]);
123+
}
124+
// audio pause / play
125+
if(kbpressed == "p") {
126+
if (document.getElementById("audio").paused) {
127+
document.getElementById("audio").play();
128+
} else {
129+
document.getElementById("audio").pause();
130+
}
131+
}
132+
// audio seek
133+
if(kbpressed == ".") {
134+
document.getElementById("audio").currentTime = document.getElementById("audio").currentTime + 5;
135+
}
136+
if(kbpressed == ",") {
137+
document.getElementById("audio").currentTime = document.getElementById("audio").currentTime - 5;
138+
}
139+
// audio volume
140+
if(kbpressed == "+") {
141+
console.log(document.getElementById("audio").volume );
142+
if (document.getElementById("audio").volume < 0.9) {
143+
document.getElementById("audio").volume = document.getElementById("audio").volume + 0.1;
144+
}
145+
}
146+
if(kbpressed == "-") {
147+
if (document.getElementById("audio").volume >= 0.1) {
148+
document.getElementById("audio").volume = document.getElementById("audio").volume - 0.1;
149+
}
101150
}
102151
if(kbpressed == "Escape") {
103152
if (modal.style == undefined) {
@@ -109,21 +158,22 @@ document.onkeydown = function (event) {
109158

110159
// play audio source
111160
function audioplay(music, element) {
161+
currentIndex = element.closest('tr').rowIndex -1;
162+
//console.log(currentIndex);
163+
console.log('Playing:', titles[element.closest('tr').rowIndex - 1]);
112164
document.getElementById("audio").pause();
113165
document.getElementById("audio").setAttribute('src', music);
114166
document.getElementById("audio").setAttribute('type', 'audio/mpeg');
115167
document.getElementById("audio").load();
116168
document.getElementById("audio").play();
117-
document.getElementById("audio").volume = 0.5;
169+
//document.getElementById("audio").volume = 0.5;
118170
document.getElementById("audio").style.visibility = "visible";
119171
// set or remove play button
120172
var data = document.getElementsByClassName("audiobutton");
121-
// reset selected up and down
122173
for (i = 0; i < data.length; i++) {
123174
data[i].style.visibility = "hidden";
124175
}
125176
for (i = 0; i < data.length; i++) {
126-
// toggle on row id
127177
if (i == element.closest('tr').rowIndex - 1) {
128178
data[i].style.visibility = "visible";
129179
}
@@ -132,6 +182,8 @@ function audioplay(music, element) {
132182
for (i = 0; i < data.length; i++) {
133183
data[i].style.visibility = "visible";
134184
}
185+
186+
currentIndex++;
135187
}
136188

137189
// play youtube audio source

0 commit comments

Comments
 (0)