txtorg commited on
Commit
62fcbbe
·
verified ·
1 Parent(s): 2e513d8

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +60 -12
public/script.js CHANGED
@@ -1,37 +1,85 @@
 
 
 
 
 
 
 
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
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const videoPlayer = document.getElementById("tvPlayer");
2
+
3
+ // Ensure iOS doesn't force fullscreen but allows manual fullscreen
4
+ videoPlayer.setAttribute("webkit-playsinline", "true");
5
+ videoPlayer.setAttribute("playsinline", "true");
6
+
7
+ // Function to switch channel and stream from backend
8
  async function changeChannel(channel) {
9
  document.getElementById("channelName").textContent = `Channel ${channel}`;
10
  document.getElementById("currentShow").textContent = `Now Playing: Loading...`;
11
 
 
12
  const streamUrl = `/hls/${channel}/index.m3u8`;
13
 
14
+ // Fetch correct timestamp from backend
15
  let seekTime = 0;
16
  try {
17
  const response = await fetch(`/timestamp?channel=${channel}`);
18
  const data = await response.json();
19
  if (data.elapsed) {
20
+ seekTime = data.elapsed; // Seek to current playback position
21
  }
22
  } catch (error) {
23
  console.error("Failed to fetch timestamp:", error);
24
  }
25
 
26
  if (Hls.isSupported()) {
27
+ if (videoPlayer.hlsInstance) {
28
+ videoPlayer.hlsInstance.destroy(); // Destroy previous HLS instance
29
+ }
30
  const hls = new Hls();
31
  hls.loadSource(streamUrl);
32
+ hls.attachMedia(videoPlayer);
33
+ videoPlayer.hlsInstance = hls; // Store instance for later cleanup
34
+
35
  hls.on(Hls.Events.MANIFEST_PARSED, () => {
36
+ videoPlayer.currentTime = seekTime; // Seek to correct position
37
+ videoPlayer.play();
38
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
39
  });
40
+ } else if (videoPlayer.canPlayType("application/vnd.apple.mpegurl")) {
41
+ videoPlayer.src = streamUrl;
42
+ videoPlayer.addEventListener("loadedmetadata", () => {
43
+ videoPlayer.currentTime = seekTime; // Seek to correct position
44
+ videoPlayer.play();
45
  document.getElementById("currentShow").textContent = `Now Playing: Channel ${channel}`;
46
  });
47
  }
48
+ }
49
+
50
+ // Toggle play/pause
51
+ function togglePlay() {
52
+ if (videoPlayer.paused) {
53
+ videoPlayer.play();
54
+ } else {
55
+ videoPlayer.pause();
56
+ }
57
+ }
58
+
59
+ // Toggle fullscreen (supports all browsers & iOS Safari)
60
+ function toggleFullscreen() {
61
+ if (document.fullscreenElement) {
62
+ document.exitFullscreen();
63
+ } else if (videoPlayer.requestFullscreen) {
64
+ videoPlayer.requestFullscreen();
65
+ } else if (videoPlayer.webkitRequestFullscreen) {
66
+ videoPlayer.webkitRequestFullscreen();
67
+ } else if (videoPlayer.msRequestFullscreen) {
68
+ videoPlayer.msRequestFullscreen();
69
+ } else if (videoPlayer.webkitEnterFullscreen) { // iOS Safari
70
+ videoPlayer.webkitEnterFullscreen();
71
+ } else {
72
+ alert("Fullscreen is not supported on this device.");
73
+ }
74
+ }
75
+
76
+ // Toggle mute/unmute
77
+ function toggleMute() {
78
+ videoPlayer.muted = !videoPlayer.muted;
79
+ document.getElementById("muteBtn").textContent = videoPlayer.muted ? "🔇 Unmute" : "🔊 Mute";
80
+ }
81
+
82
+ // Load default channel on page load
83
+ window.onload = () => {
84
+ changeChannel(1);
85
+ };