Update public/script.js
Browse files- public/script.js +45 -42
public/script.js
CHANGED
|
@@ -65,25 +65,29 @@ async function changeChannel(channel) {
|
|
| 65 |
}
|
| 66 |
|
| 67 |
const hls = new Hls({
|
| 68 |
-
maxBufferLength:
|
| 69 |
-
maxMaxBufferLength:
|
| 70 |
-
maxBufferSize:
|
| 71 |
-
liveSyncDurationCount:
|
| 72 |
});
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
if (seekTime > 0 && !videoPlayer._seeked) {
|
| 84 |
console.log("Seeking to:", seekTime);
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
});
|
| 89 |
|
|
@@ -94,52 +98,51 @@ hls.on(Hls.Events.ERROR, (event, data) => {
|
|
| 94 |
if (data.fatal) {
|
| 95 |
switch (data.type) {
|
| 96 |
case Hls.ErrorTypes.NETWORK_ERROR:
|
| 97 |
-
console.warn("Network error, retrying...");
|
| 98 |
hls.stopLoad();
|
| 99 |
setTimeout(() => hls.startLoad(), 3000);
|
| 100 |
break;
|
|
|
|
| 101 |
case Hls.ErrorTypes.MEDIA_ERROR:
|
| 102 |
if (retryCount < maxRetries) {
|
| 103 |
-
console.warn(
|
| 104 |
hls.recoverMediaError();
|
| 105 |
retryCount++;
|
| 106 |
} else {
|
| 107 |
-
console.error("Max retries reached, reloading stream...");
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
break;
|
|
|
|
| 113 |
default:
|
| 114 |
-
console.error("Critical error,
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
break;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
});
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
if (Math.abs(videoPlayer.currentTime - seekTime) > 1) {
|
| 135 |
-
videoPlayer.currentTime = seekTime;
|
| 136 |
-
}
|
| 137 |
-
videoPlayer.play().catch(err => console.error("Playback error:", err));
|
| 138 |
-
});
|
| 139 |
|
| 140 |
-
|
| 141 |
-
}
|
| 142 |
-
}
|
| 143 |
|
| 144 |
function togglePlay() {
|
| 145 |
if (videoPlayer.paused) {
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
const hls = new Hls({
|
| 68 |
+
maxBufferLength: 30,
|
| 69 |
+
maxMaxBufferLength: 60,
|
| 70 |
+
maxBufferSize: 50 * 1000 * 1000,
|
| 71 |
+
liveSyncDurationCount: 2
|
| 72 |
});
|
| 73 |
|
| 74 |
+
hls.loadSource(streamUrl);
|
| 75 |
+
hls.attachMedia(videoPlayer);
|
| 76 |
+
videoPlayer.hlsInstance = hls;
|
| 77 |
|
| 78 |
+
hls.on(Hls.Events.MANIFEST_PARSED, () => {
|
| 79 |
+
videoPlayer.play().catch(err => console.error("Playback error:", err));
|
| 80 |
+
});
|
| 81 |
|
| 82 |
+
hls.on(Hls.Events.BUFFER_APPENDED, () => {
|
| 83 |
if (seekTime > 0 && !videoPlayer._seeked) {
|
| 84 |
console.log("Seeking to:", seekTime);
|
| 85 |
+
setTimeout(() => {
|
| 86 |
+
if (Math.abs(videoPlayer.currentTime - seekTime) > 1) {
|
| 87 |
+
videoPlayer.currentTime = seekTime;
|
| 88 |
+
}
|
| 89 |
+
}, 1000);
|
| 90 |
+
videoPlayer._seeked = true;
|
| 91 |
}
|
| 92 |
});
|
| 93 |
|
|
|
|
| 98 |
if (data.fatal) {
|
| 99 |
switch (data.type) {
|
| 100 |
case Hls.ErrorTypes.NETWORK_ERROR:
|
| 101 |
+
console.warn("Network error, retrying in 3s...");
|
| 102 |
hls.stopLoad();
|
| 103 |
setTimeout(() => hls.startLoad(), 3000);
|
| 104 |
break;
|
| 105 |
+
|
| 106 |
case Hls.ErrorTypes.MEDIA_ERROR:
|
| 107 |
if (retryCount < maxRetries) {
|
| 108 |
+
console.warn(`Media error, attempting recovery... (Retry ${retryCount + 1}/${maxRetries})`);
|
| 109 |
hls.recoverMediaError();
|
| 110 |
retryCount++;
|
| 111 |
} else {
|
| 112 |
+
console.error("Max retries reached, reloading stream after 5s...");
|
| 113 |
+
setTimeout(() => {
|
| 114 |
+
hls.loadSource(`/hls/${currentChannel}/index.m3u8`);
|
| 115 |
+
hls.startLoad();
|
| 116 |
+
retryCount = 0;
|
| 117 |
+
}, 5000);
|
| 118 |
}
|
| 119 |
break;
|
| 120 |
+
|
| 121 |
default:
|
| 122 |
+
console.error("Critical error, waiting before reloading...");
|
| 123 |
+
setTimeout(() => {
|
| 124 |
+
hls.loadSource(`/hls/${currentChannel}/index.m3u8`);
|
| 125 |
+
hls.startLoad();
|
| 126 |
+
retryCount = 0;
|
| 127 |
+
}, 5000);
|
| 128 |
break;
|
| 129 |
}
|
| 130 |
}
|
| 131 |
});
|
| 132 |
|
| 133 |
+
// Improved handling of video stalling
|
| 134 |
+
videoPlayer.addEventListener("waiting", () => {
|
| 135 |
+
console.warn("Video stalled, checking buffer...");
|
| 136 |
+
|
| 137 |
+
setTimeout(() => {
|
| 138 |
+
if (videoPlayer.readyState < 2) {
|
| 139 |
+
console.warn("Still stalled, attempting recovery...");
|
| 140 |
+
hls.recoverMediaError();
|
| 141 |
+
}
|
| 142 |
+
}, 3000);
|
| 143 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
|
|
|
|
|
|
|
| 146 |
|
| 147 |
function togglePlay() {
|
| 148 |
if (videoPlayer.paused) {
|