File size: 9,313 Bytes
d0c3311 25dca33 831e55c 2a108da 25dca33 ded211a 2a108da d0c3311 25dca33 2a108da 25dca33 ded211a 25dca33 831e55c 2a108da d0c3311 2a108da 25dca33 2a108da ded211a 2a108da d0c3311 25dca33 ded211a 2a108da d0c3311 25dca33 ded211a 2a108da d0c3311 25dca33 ded211a 25dca33 831e55c d0c3311 25dca33 d0c3311 25dca33 d0c3311 ded211a d0c3311 ded211a d0c3311 25dca33 ded211a 25dca33 2a108da 25dca33 ded211a 2a108da d0c3311 831e55c 25dca33 2a108da 25dca33 ded211a 2a108da ded211a 25dca33 ded211a 25dca33 ded211a 831e55c ded211a 25dca33 d0c3311 25dca33 d0c3311 831e55c d0c3311 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
"use client";
import { useEffect, useRef, useState } from "react";
import "./MoviePlayer.css";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faFastBackward,
faFastForward,
faPause,
faPlay,
faVolumeUp,
faVolumeMute,
faExpand,
faDownload,
faArrowLeft,
faCaretLeft,
faAngleLeft,
} from "@fortawesome/free-solid-svg-icons";
import { Spinner } from "@/components/Spinner";
import SeekableProgressBar from "./SeekableProgressBar";
export default function MoviePlayer({ videoUrl, title }) {
const videoRef = useRef(null);
const [isPlaying, setIsPlaying] = useState(false);
const [volume, setVolume] = useState(1);
const [isMuted, setIsMuted] = useState(false);
const [progress, setProgress] = useState(0);
const [buffer, setBuffer] = useState(0); // Buffer state
const [isFullscreen, setIsFullscreen] = useState(false);
const [showControls, setShowControls] = useState(true);
const [isBuffering, setIsBuffering] = useState(false);
const overlayTimeout = useRef(null);
const [contextMenu, setContextMenu] = useState({ visible: false, x: 0, y: 0 });
const playerVersion = "0.0.2 Alpha";
useEffect(() => {
const videoElement = videoRef.current;
const handlePlay = () => setIsPlaying(true);
const handlePause = () => setIsPlaying(false);
const handleTimeUpdate = () => {
setProgress((videoElement.currentTime / videoElement.duration) * 100);
updateBuffer(); // Update buffer on time update
};
const handleWaiting = () => setIsBuffering(true);
const handlePlaying = () => setIsBuffering(false);
const handleProgress = () => updateBuffer(); // Update buffer on progress
videoElement.addEventListener("play", handlePlay);
videoElement.addEventListener("pause", handlePause);
videoElement.addEventListener("timeupdate", handleTimeUpdate);
videoElement.addEventListener("waiting", handleWaiting);
videoElement.addEventListener("playing", handlePlaying);
videoElement.addEventListener("progress", handleProgress); // Listen to progress events
return () => {
videoElement.removeEventListener("play", handlePlay);
videoElement.removeEventListener("pause", handlePause);
videoElement.removeEventListener("timeupdate", handleTimeUpdate);
videoElement.removeEventListener("waiting", handleWaiting);
videoElement.removeEventListener("playing", handlePlaying);
videoElement.removeEventListener("progress", handleProgress); // Clean up progress listener
};
}, []);
useEffect(() => {
if (showControls) {
if (overlayTimeout.current) {
clearTimeout(overlayTimeout.current);
}
overlayTimeout.current = setTimeout(() => setShowControls(false), 3000);
}
return () => clearTimeout(overlayTimeout.current);
}, [showControls]);
const handleContextMenu = (event) => {
event.preventDefault();
setContextMenu({
visible: true,
x: event.pageX,
y: event.pageY,
});
};
const hideContextMenu = () => {
setContextMenu({ visible: false, x: 0, y: 0 });
};
useEffect(() => {
window.addEventListener("click", hideContextMenu);
return () => {
window.removeEventListener("click", hideContextMenu);
};
}, []);
const handleFastForward = () => {
if (videoRef.current) {
videoRef.current.currentTime = Math.min(
videoRef.current.duration,
videoRef.current.currentTime + 10
);
}
};
const handleRewind = () => {
if (videoRef.current) {
videoRef.current.currentTime = Math.max(
0,
videoRef.current.currentTime - 10
);
}
};
const togglePlayPause = () => {
if (isPlaying) {
videoRef.current.pause();
setShowControls(true);
} else {
videoRef.current.play();
setShowControls(false);
}
};
const handleVolumeChange = (event) => {
const volumeValue = event.target.value;
setVolume(volumeValue);
videoRef.current.volume = volumeValue;
setIsMuted(volumeValue === 0);
};
const toggleMute = () => {
if (isMuted) {
videoRef.current.volume = volume;
setIsMuted(false);
} else {
videoRef.current.volume = 0;
setIsMuted(true);
}
};
const toggleFullscreen = () => {
const doc = window.document;
const docEl = doc.documentElement;
const requestFullscreen =
docEl.requestFullscreen ||
docEl.mozRequestFullScreen ||
docEl.webkitRequestFullscreen ||
docEl.msRequestFullscreen;
const exitFullscreen =
doc.exitFullscreen ||
doc.mozCancelFullScreen ||
docEl.webkitExitFullscreen ||
doc.msExitFullscreen;
if (!isFullscreen) {
requestFullscreen.call(docEl);
} else {
exitFullscreen.call(doc);
}
setIsFullscreen(!isFullscreen);
};
const handleSeek = (newProgress) => {
videoRef.current.currentTime = (newProgress / 100) * videoRef.current.duration;
setProgress(newProgress);
};
const handleMouseMove = () => {
setShowControls(true);
};
const formatTime = (seconds) => {
if (isNaN(seconds)) {
return '00:00:00';
}
const wholeSeconds = Math.floor(seconds);
const hours = Math.floor(wholeSeconds / 3600);
const minutes = Math.floor((wholeSeconds % 3600) / 60);
const secs = wholeSeconds % 60;
const formattedHours = String(hours).padStart(2, '0');
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(secs).padStart(2, '0');
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
};
const updateBuffer = () => {
const videoElement = videoRef.current;
if (videoElement.buffered.length > 0) {
const bufferEnd = videoElement.buffered.end(videoElement.buffered.length - 1);
const bufferValue = (bufferEnd / videoElement.duration) * 100;
setBuffer(bufferValue);
}
};
return (
<div className="video-player-container" onMouseMove={handleMouseMove} onContextMenu={handleContextMenu}>
<div className={`player-overlay ${showControls ? "show" : "hide"}`}>
<h2 className="video-title"><div className="control-btn"><FontAwesomeIcon icon={faAngleLeft} size="lg"/></div>{title}</h2>
<div className="controls">
<div className="player-controls-top">
<label className="current-time">{formatTime(videoRef?.current?.currentTime)}</label>
<SeekableProgressBar progress={progress} buffer={buffer} onSeek={handleSeek} />
<label className="duration">{formatTime(videoRef?.current?.duration)}</label>
</div>
<div className="player-controls-down">
<div className="player-controls-left">
<button
onClick={handleRewind}
className="control-btn"
disabled={!isPlaying}
>
<FontAwesomeIcon icon={faFastBackward} size="xl" />
</button>
<button onClick={togglePlayPause} className="play-pause-btn">
{isPlaying ? (
<FontAwesomeIcon icon={faPause} size="xl" />
) : (
<FontAwesomeIcon icon={faPlay} size="xl" />
)}
</button>
<button
onClick={handleFastForward}
className="control-btn"
disabled={!isPlaying}
>
<FontAwesomeIcon icon={faFastForward} size="xl" />
</button>
</div>
<div className="player-controls-right">
<button onClick={toggleMute} className="control-btn">
<FontAwesomeIcon
icon={isMuted ? faVolumeMute : faVolumeUp}
size="xl"
/>
</button>
<input
type="range"
className="volume-control"
min="0"
max="1"
step="0.01"
value={volume}
onChange={handleVolumeChange}
/>
<button onClick={toggleFullscreen} className="control-btn">
<FontAwesomeIcon icon={faExpand} size="xl" />
</button>
{videoUrl && (
<a href={videoUrl} download className="control-btn">
<FontAwesomeIcon icon={faDownload} size="xl" />
</a>
)}
</div>
</div>
</div>
</div>
<video
ref={videoRef}
className="video-element"
controls={false}
preload="auto"
src={videoUrl}
>
<track
kind="subtitles"
label="English"
srcLang="en"
src="path/to/your/subtitles.vtt" // Replace with actual subtitles URL
/>
Your browser does not support the video tag.
</video>
{isBuffering && <div className="buffering-indicator"><div className="postion-fix"><Spinner/></div></div>}
{contextMenu.visible && (
<div
className="context-menu"
style={{ left: contextMenu.x, top: contextMenu.y }}
>
<ul>
<li>Player Version: {playerVersion}</li>
</ul>
</div>
)}
</div>
);
}
|