-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (130 loc) · 4.47 KB
/
Copy pathscript.js
File metadata and controls
147 lines (130 loc) · 4.47 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
let audioContext;
let sourceElement = new Audio();
let sourceNode;
let playlist = [];
let currentIndex = 0;
const existingSongs = [
"You're Gonna Go Far, Kid 4.mp3",
"Devil - Ironmouse & Bubi (Official Music Video) 4.mp3",
"LiSA _QUEEN_ × TV anime _Shangri-La Frontier_ 2nd season 1st part Collaboration Movie 4.mp3",
"yama - 春を告げる (Official Video) 4.mp3"
];
// Load existing songs into the playlist on page load
window.addEventListener('DOMContentLoaded', () => {
existingSongs.forEach(song => {
// Create a File-like object for consistency with uploaded files
playlist.push({ name: song, url: song });
});
updatePlaylistUI();
});
function handleFileSelect(e) {
playlist = Array.from(e.target.files);
if (playlist.length > 0) {
currentIndex = 0;
playSong(currentIndex);
} else {
updatePlaylistUI();
}
}
function playSong(index) {
const file = playlist[index];
if (file) {
const url = file.url ? file.url : URL.createObjectURL(file);
sourceElement.src = url;
sourceElement.load();
sourceElement.play();
document.getElementById('playPauseBtn').textContent = '⏸';
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
sourceNode = audioContext.createMediaElementSource(sourceElement);
sourceNode.connect(audioContext.destination);
}
updatePlaylistUI();
document.getElementById('nowPlaying').textContent = file.name;
}
}
function updatePlaylistUI() {
const trackList = document.getElementById('trackList');
trackList.innerHTML = '';
if (playlist.length === 0) {
trackList.innerHTML = '<div class="panel-message">No tracks uploaded yet</div>';
return;
}
playlist.forEach((file, idx) => {
const div = document.createElement('div');
div.className = 'playlist-item' + (idx === currentIndex ? ' active' : '');
div.textContent = file.name;
div.addEventListener('click', () => {
currentIndex = idx;
playSong(currentIndex);
updatePlaylistUI();
});
trackList.appendChild(div);
});
}
function play() {
sourceElement.play();
document.getElementById('playPauseBtn').textContent = '⏸';
}
function pause() {
sourceElement.pause();
document.getElementById('playPauseBtn').textContent = '▶';
}
function skip(seconds) {
if (sourceElement.currentTime !== undefined) {
sourceElement.currentTime = Math.min(sourceElement.duration, sourceElement.currentTime + seconds);
}
}
function rewind(seconds) {
if (sourceElement.currentTime !== undefined) {
sourceElement.currentTime = Math.max(0, sourceElement.currentTime - seconds);
}
}
// Volume control
document.getElementById('volumeSlider').addEventListener('input', function(e) {
sourceElement.volume = parseFloat(e.target.value);
});
document.getElementById('playPauseBtn').addEventListener('click', function() {
if (sourceElement.paused) {
play();
} else {
pause();
}
});
document.getElementById('rewindBtn').addEventListener('click', function() {
rewind(10); // rewinds 10 seconds
});
document.getElementById('skipBtn').addEventListener('click', function() {
skip(10); // skips forward 10 seconds
});
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
document.getElementById('nextBtn').addEventListener('click', function() {
if (playlist.length > 0) {
currentIndex = (currentIndex + 1) % playlist.length;
playSong(currentIndex);
}
});
document.getElementById('prevBtn').addEventListener('click', function() {
if (playlist.length > 0) {
currentIndex = (currentIndex - 1 + playlist.length) % playlist.length;
playSong(currentIndex);
}
});
const dropArea = document.getElementById('dropArea');
dropArea.addEventListener('dragover', (e) => {
e.preventDefault();
dropArea.classList.add('dragover');
});
dropArea.addEventListener('dragleave', (e) => {
e.preventDefault();
dropArea.classList.remove('dragover');
});
dropArea.addEventListener('drop', (e) => {
e.preventDefault();
dropArea.classList.remove('dragover');
document.getElementById('fileInput').files = e.dataTransfer.files;
document.getElementById('fileInput').dispatchEvent(new Event('change'));
});
dropArea.addEventListener('click', () => {
document.getElementById('fileInput').click();
});