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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Video Player Application
|
| 2 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 3 |
+
const video = document.getElementById('videoPlayer');
|
| 4 |
+
const playlist = document.getElementById('playlist');
|
| 5 |
+
const overlay = document.getElementById('videoOverlay');
|
| 6 |
+
const currentTitle = document.getElementById('currentTitle');
|
| 7 |
+
const playPauseBtn = document.getElementById('playPauseBtn');
|
| 8 |
+
const prevBtn = document.getElementById('prevBtn');
|
| 9 |
+
const nextBtn = document.getElementById('nextBtn');
|
| 10 |
+
const muteBtn = document.getElementById('muteBtn');
|
| 11 |
+
const volumeSlider = document.getElementById('volumeSlider');
|
| 12 |
+
const fullscreenBtn = document.getElementById('fullscreenBtn');
|
| 13 |
+
|
| 14 |
+
let videos = [];
|
| 15 |
+
let currentIndex = -1;
|
| 16 |
+
|
| 17 |
+
// Load playlist from API
|
| 18 |
+
async function loadPlaylist() {
|
| 19 |
+
try {
|
| 20 |
+
const response = await fetch('/api/playlist');
|
| 21 |
+
const data = await response.json();
|
| 22 |
+
videos = data.videos;
|
| 23 |
+
renderPlaylist();
|
| 24 |
+
} catch (error) {
|
| 25 |
+
console.error('Failed to load playlist:', error);
|
| 26 |
+
playlist.innerHTML = `
|
| 27 |
+
<div class="empty-playlist">
|
| 28 |
+
<span>β</span>
|
| 29 |
+
<p>Failed to load playlist</p>
|
| 30 |
+
</div>
|
| 31 |
+
`;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Render playlist items
|
| 36 |
+
function renderPlaylist() {
|
| 37 |
+
if (videos.length === 0) {
|
| 38 |
+
playlist.innerHTML = `
|
| 39 |
+
<div class="empty-playlist">
|
| 40 |
+
<span>π</span>
|
| 41 |
+
<p>No videos found in playlist folder</p>
|
| 42 |
+
</div>
|
| 43 |
+
`;
|
| 44 |
+
return;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
playlist.innerHTML = videos.map((video, index) => `
|
| 48 |
+
<div class="playlist-item ${index === currentIndex ? 'active' : ''}"
|
| 49 |
+
data-index="${index}"
|
| 50 |
+
onclick="playVideo(${index})">
|
| 51 |
+
<span class="item-icon">π¬</span>
|
| 52 |
+
<div class="item-info">
|
| 53 |
+
<div class="item-title" title="${video.name}">${video.title}</div>
|
| 54 |
+
<div class="item-size">${video.size_human}</div>
|
| 55 |
+
</div>
|
| 56 |
+
</div>
|
| 57 |
+
`).join('');
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Play video at index
|
| 61 |
+
window.playVideo = function(index) {
|
| 62 |
+
if (index < 0 || index >= videos.length) return;
|
| 63 |
+
|
| 64 |
+
currentIndex = index;
|
| 65 |
+
const videoData = videos[index];
|
| 66 |
+
|
| 67 |
+
video.src = videoData.path;
|
| 68 |
+
video.load();
|
| 69 |
+
video.play().catch(e => console.log('Autoplay prevented:', e));
|
| 70 |
+
|
| 71 |
+
currentTitle.textContent = videoData.title;
|
| 72 |
+
overlay.classList.add('hidden');
|
| 73 |
+
|
| 74 |
+
// Update playlist highlight
|
| 75 |
+
document.querySelectorAll('.playlist-item').forEach((item, i) => {
|
| 76 |
+
item.classList.toggle('active', i === index);
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
updatePlayPauseButton();
|
| 80 |
+
};
|
| 81 |
+
|
| 82 |
+
// Play/Pause toggle
|
| 83 |
+
function togglePlayPause() {
|
| 84 |
+
if (video.paused) {
|
| 85 |
+
video.play().catch(e => console.log('Play failed:', e));
|
| 86 |
+
} else {
|
| 87 |
+
video.pause();
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
// Update play/pause button icon
|
| 92 |
+
function updatePlayPauseButton() {
|
| 93 |
+
playPauseBtn.textContent = video.paused ? 'βΆ' : 'βΈ';
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Play previous video
|
| 97 |
+
function playPrevious() {
|
| 98 |
+
if (videos.length === 0) return;
|
| 99 |
+
const newIndex = currentIndex > 0 ? currentIndex - 1 : videos.length - 1;
|
| 100 |
+
playVideo(newIndex);
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// Play next video
|
| 104 |
+
function playNext() {
|
| 105 |
+
if (videos.length === 0) return;
|
| 106 |
+
const newIndex = currentIndex < videos.length - 1 ? currentIndex + 1 : 0;
|
| 107 |
+
playVideo(newIndex);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Toggle mute
|
| 111 |
+
function toggleMute() {
|
| 112 |
+
video.muted = !video.muted;
|
| 113 |
+
muteBtn.textContent = video.muted ? 'π' : 'π';
|
| 114 |
+
volumeSlider.value = video.muted ? 0 : video.volume;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// Update volume
|
| 118 |
+
function updateVolume() {
|
| 119 |
+
video.volume = volumeSlider.value;
|
| 120 |
+
video.muted = video.volume === 0;
|
| 121 |
+
muteBtn.textContent = video.muted ? 'π' : 'π';
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
// Toggle fullscreen
|
| 125 |
+
function toggleFullscreen() {
|
| 126 |
+
const container = document.querySelector('.video-container');
|
| 127 |
+
|
| 128 |
+
if (document.fullscreenElement) {
|
| 129 |
+
document.exitFullscreen();
|
| 130 |
+
} else if (container.requestFullscreen) {
|
| 131 |
+
container.requestFullscreen();
|
| 132 |
+
} else if (container.webkitRequestFullscreen) {
|
| 133 |
+
container.webkitRequestFullscreen();
|
| 134 |
+
}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
// Event listeners
|
| 138 |
+
playPauseBtn.addEventListener('click', togglePlayPause);
|
| 139 |
+
prevBtn.addEventListener('click', playPrevious);
|
| 140 |
+
nextBtn.addEventListener('click', playNext);
|
| 141 |
+
muteBtn.addEventListener('click', toggleMute);
|
| 142 |
+
volumeSlider.addEventListener('input', updateVolume);
|
| 143 |
+
fullscreenBtn.addEventListener('click', toggleFullscreen);
|
| 144 |
+
|
| 145 |
+
video.addEventListener('play', updatePlayPauseButton);
|
| 146 |
+
video.addEventListener('pause', updatePlayPauseButton);
|
| 147 |
+
video.addEventListener('ended', playNext);
|
| 148 |
+
|
| 149 |
+
// Keyboard shortcuts
|
| 150 |
+
document.addEventListener('keydown', function(e) {
|
| 151 |
+
if (e.target.tagName === 'INPUT') return;
|
| 152 |
+
|
| 153 |
+
switch(e.key) {
|
| 154 |
+
case ' ':
|
| 155 |
+
case 'k':
|
| 156 |
+
e.preventDefault();
|
| 157 |
+
togglePlayPause();
|
| 158 |
+
break;
|
| 159 |
+
case 'ArrowLeft':
|
| 160 |
+
e.preventDefault();
|
| 161 |
+
video.currentTime = Math.max(0, video.currentTime - 10);
|
| 162 |
+
break;
|
| 163 |
+
case 'ArrowRight':
|
| 164 |
+
e.preventDefault();
|
| 165 |
+
video.currentTime = Math.min(video.duration, video.currentTime + 10);
|
| 166 |
+
break;
|
| 167 |
+
case 'ArrowUp':
|
| 168 |
+
e.preventDefault();
|
| 169 |
+
video.volume = Math.min(1, video.volume + 0.1);
|
| 170 |
+
volumeSlider.value = video.volume;
|
| 171 |
+
break;
|
| 172 |
+
case 'ArrowDown':
|
| 173 |
+
e.preventDefault();
|
| 174 |
+
video.volume = Math.max(0, video.volume - 0.1);
|
| 175 |
+
volumeSlider.value = video.volume;
|
| 176 |
+
break;
|
| 177 |
+
case 'f':
|
| 178 |
+
e.preventDefault();
|
| 179 |
+
toggleFullscreen();
|
| 180 |
+
break;
|
| 181 |
+
case 'm':
|
| 182 |
+
e.preventDefault();
|
| 183 |
+
toggleMute();
|
| 184 |
+
break;
|
| 185 |
+
case 'n':
|
| 186 |
+
e.preventDefault();
|
| 187 |
+
playNext();
|
| 188 |
+
break;
|
| 189 |
+
case 'p':
|
| 190 |
+
e.preventDefault();
|
| 191 |
+
playPrevious();
|
| 192 |
+
break;
|
| 193 |
+
}
|
| 194 |
+
});
|
| 195 |
+
|
| 196 |
+
// Initialize
|
| 197 |
+
loadPlaylist();
|
| 198 |
+
});
|