txtorg commited on
Commit
38cdffc
·
verified ·
1 Parent(s): 224116f

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +16 -28
public/script.js CHANGED
@@ -1,49 +1,37 @@
1
- async function fetchChannels() {
2
- const response = await fetch("/channels");
3
- const data = await response.json();
4
-
5
- const channelButtons = document.querySelector(".channel-buttons");
6
- channelButtons.innerHTML = ""; // Clear existing buttons
7
-
8
- data.channels.forEach(channel => {
9
- const button = document.createElement("button");
10
- button.textContent = `Channel ${channel}`;
11
- button.onclick = () => changeChannel(channel);
12
- channelButtons.appendChild(button);
13
- });
14
-
15
- // Set default channel to the first available one
16
- if (data.channels.length > 0) {
17
- changeChannel(data.channels[0]);
18
- } else {
19
- document.getElementById("channelName").textContent = "No channels available";
20
- document.getElementById("currentShow").textContent = "No active streams.";
21
- }
22
- }
23
-
24
- function changeChannel(channel) {
25
  document.getElementById("channelName").textContent = `Channel ${channel}`;
26
  document.getElementById("currentShow").textContent = `Now Playing: Loading...`;
27
 
28
  const video = document.getElementById("tvPlayer");
29
  const streamUrl = `/hls/${channel}/index.m3u8`;
30
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  if (Hls.isSupported()) {
32
  const hls = new Hls();
33
  hls.loadSource(streamUrl);
34
  hls.attachMedia(video);
35
  hls.on(Hls.Events.MANIFEST_PARSED, () => {
 
36
  video.play();
37
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
38
  });
39
  } else if (video.canPlayType("application/vnd.apple.mpegurl")) {
40
  video.src = streamUrl;
41
  video.addEventListener("loadedmetadata", () => {
 
42
  video.play();
43
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
44
  });
45
  }
46
- }
47
-
48
- // Fetch channels on page load
49
- window.onload = fetchChannels;
 
1
+ async function changeChannel(channel) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  document.getElementById("channelName").textContent = `Channel ${channel}`;
3
  document.getElementById("currentShow").textContent = `Now Playing: Loading...`;
4
 
5
  const video = document.getElementById("tvPlayer");
6
  const streamUrl = `/hls/${channel}/index.m3u8`;
7
 
8
+ // Get correct start time from backend
9
+ let seekTime = 0;
10
+ try {
11
+ const response = await fetch(`/timestamp?channel=${channel}`);
12
+ const data = await response.json();
13
+ if (data.elapsed) {
14
+ seekTime = data.elapsed; // How far into the stream we should be
15
+ }
16
+ } catch (error) {
17
+ console.error("Failed to fetch timestamp:", error);
18
+ }
19
+
20
  if (Hls.isSupported()) {
21
  const hls = new Hls();
22
  hls.loadSource(streamUrl);
23
  hls.attachMedia(video);
24
  hls.on(Hls.Events.MANIFEST_PARSED, () => {
25
+ video.currentTime = seekTime; // Seek to correct position
26
  video.play();
27
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
28
  });
29
  } else if (video.canPlayType("application/vnd.apple.mpegurl")) {
30
  video.src = streamUrl;
31
  video.addEventListener("loadedmetadata", () => {
32
+ video.currentTime = seekTime; // Seek to correct position
33
  video.play();
34
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
35
  });
36
  }
37
+ }