This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hicup-player.js
80 lines (71 loc) · 2.21 KB
/
hicup-player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let audioFiles = [];
let currentSongIndex = 0;
let shuffledIndexes = [];
const audio = new Audio();
audio.addEventListener('ended', function() {
if (currentSongIndex < audioFiles.length - 1) {
currentSongIndex++;
} else {
currentSongIndex = 0;
}
audio.src = URL.createObjectURL(audioFiles[currentSongIndex]);
audio.play();
updateDisplay();
});
document.getElementById('play').addEventListener('click', function() {
if (audio.paused) {
audio.play();
updateDisplay();
} else {
audio.pause();
updateDisplay('Paused');
}
});
document.getElementById('prev').addEventListener('click', function() {
if (currentSongIndex > 0) {
currentSongIndex--;
audio.src = URL.createObjectURL(audioFiles[currentSongIndex]);
audio.play();
updateDisplay();
}
});
document.getElementById('next').addEventListener('click', function() {
if (currentSongIndex < audioFiles.length - 1) {
currentSongIndex++;
audio.src = URL.createObjectURL(audioFiles[currentSongIndex]);
audio.play();
updateDisplay();
}
});
document.getElementById('shuffle').addEventListener('click', function() {
shuffleArray(shuffledIndexes);
currentSongIndex = 0;
audio.src = URL.createObjectURL(audioFiles[shuffledIndexes[currentSongIndex]]);
audio.play();
updateDisplay();
});
document.getElementById('file-input').addEventListener('change', function(event) {
audioFiles = Array.from(event.target.files);
shuffledIndexes = [...Array(audioFiles.length).keys()];
resetAudio();
});
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function resetAudio() {
if (audioFiles.length > 0) {
currentSongIndex = 0;
audio.src = URL.createObjectURL(audioFiles[currentSongIndex]);
audio.play();
updateDisplay();
} else {
audio.src = '';
updateDisplay('Hicup Music Player');
}
}
function updateDisplay(text = '') {
document.getElementById('screen').innerText = text || `Now Playing: ${audioFiles[currentSongIndex].name}`;
}