Update public/script.js
Browse files- public/script.js +28 -3
public/script.js
CHANGED
|
@@ -54,12 +54,15 @@ async function changeChannel(channel) {
|
|
| 54 |
const response = await fetch(`/timestamp?channel=${channel}`);
|
| 55 |
const data = await response.json();
|
| 56 |
if (data.elapsed) {
|
| 57 |
-
seekTime = data.elapsed;
|
| 58 |
}
|
| 59 |
} catch (error) {
|
| 60 |
console.error("Failed to fetch timestamp:", error);
|
| 61 |
}
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
// Store selected channel in localStorage
|
| 64 |
localStorage.setItem(LAST_CHANNEL_KEY, channel);
|
| 65 |
|
|
@@ -77,13 +80,35 @@ async function changeChannel(channel) {
|
|
| 77 |
});
|
| 78 |
|
| 79 |
hls.on(Hls.Events.LEVEL_LOADED, () => {
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
});
|
| 82 |
|
| 83 |
document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
|
| 84 |
} else if (videoPlayer.canPlayType("application/vnd.apple.mpegurl")) {
|
| 85 |
videoPlayer.src = streamUrl;
|
| 86 |
-
videoPlayer.addEventListener("
|
| 87 |
videoPlayer.currentTime = seekTime;
|
| 88 |
videoPlayer.play().catch(err => console.error("Playback error:", err));
|
| 89 |
});
|
|
|
|
| 54 |
const response = await fetch(`/timestamp?channel=${channel}`);
|
| 55 |
const data = await response.json();
|
| 56 |
if (data.elapsed) {
|
| 57 |
+
seekTime = data.elapsed;
|
| 58 |
}
|
| 59 |
} catch (error) {
|
| 60 |
console.error("Failed to fetch timestamp:", error);
|
| 61 |
}
|
| 62 |
|
| 63 |
+
// Prevent invalid seek times
|
| 64 |
+
if (seekTime < 0 || isNaN(seekTime)) seekTime = 0;
|
| 65 |
+
|
| 66 |
// Store selected channel in localStorage
|
| 67 |
localStorage.setItem(LAST_CHANNEL_KEY, channel);
|
| 68 |
|
|
|
|
| 80 |
});
|
| 81 |
|
| 82 |
hls.on(Hls.Events.LEVEL_LOADED, () => {
|
| 83 |
+
setTimeout(() => {
|
| 84 |
+
videoPlayer.currentTime = seekTime; // Ensure proper seeking
|
| 85 |
+
}, 1000); // Small delay to allow buffer
|
| 86 |
+
});
|
| 87 |
+
|
| 88 |
+
// Auto-recovery on errors
|
| 89 |
+
hls.on(Hls.Events.ERROR, (event, data) => {
|
| 90 |
+
if (data.fatal) {
|
| 91 |
+
switch (data.type) {
|
| 92 |
+
case Hls.ErrorTypes.NETWORK_ERROR:
|
| 93 |
+
console.warn("Network error, trying to recover...");
|
| 94 |
+
hls.startLoad();
|
| 95 |
+
break;
|
| 96 |
+
case Hls.ErrorTypes.MEDIA_ERROR:
|
| 97 |
+
console.warn("Media error, attempting recovery...");
|
| 98 |
+
hls.recoverMediaError();
|
| 99 |
+
break;
|
| 100 |
+
default:
|
| 101 |
+
console.error("Unrecoverable error, reloading video...");
|
| 102 |
+
changeChannel(channel); // Restart playback
|
| 103 |
+
break;
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
});
|
| 107 |
|
| 108 |
document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
|
| 109 |
} else if (videoPlayer.canPlayType("application/vnd.apple.mpegurl")) {
|
| 110 |
videoPlayer.src = streamUrl;
|
| 111 |
+
videoPlayer.addEventListener("loadedmetadata", () => {
|
| 112 |
videoPlayer.currentTime = seekTime;
|
| 113 |
videoPlayer.play().catch(err => console.error("Playback error:", err));
|
| 114 |
});
|