From 6a6221db7996a2333697ec94ef5efc9b1ace1302 Mon Sep 17 00:00:00 2001 From: ParagGhatage Date: Tue, 18 Feb 2025 00:35:03 +0530 Subject: [PATCH] Commits on Feb 13, 2025 feat(video): Added time tracking, seek functionality, and preload fix. --- .../VideoPlayer/NetflixStylePlayer.tsx | 222 +++++------------- 1 file changed, 56 insertions(+), 166 deletions(-) diff --git a/frontend/src/components/VideoPlayer/NetflixStylePlayer.tsx b/frontend/src/components/VideoPlayer/NetflixStylePlayer.tsx index e385529f..f6ec602e 100644 --- a/frontend/src/components/VideoPlayer/NetflixStylePlayer.tsx +++ b/frontend/src/components/VideoPlayer/NetflixStylePlayer.tsx @@ -18,26 +18,23 @@ interface NetflixStylePlayerProps { description: string; } -export default function NetflixStylePlayer({ - videoSrc, - title, - description, -}: NetflixStylePlayerProps) { +export default function NetflixStylePlayer({videoSrc}: NetflixStylePlayerProps) { const [isPlaying, setIsPlaying] = useState(false); const [progress, setProgress] = useState(0); const [volume, setVolume] = useState(1); const [isMuted, setIsMuted] = useState(false); - const [showControls, setShowControls] = useState(false); // Initially hidden + const [showControls, setShowControls] = useState(false); const [isFullscreen, setIsFullscreen] = useState(false); const videoRef = useRef(null); const containerRef = useRef(null); + useEffect(() => { let timeout: NodeJS.Timeout; const showControlsTemporarily = () => { setShowControls(true); clearTimeout(timeout); - timeout = setTimeout(() => setShowControls(false), 3000); // Hide after 3 seconds + timeout = setTimeout(() => setShowControls(false), 3000); }; const container = containerRef.current; @@ -55,31 +52,34 @@ export default function NetflixStylePlayer({ }; }, []); + const formatTime = (timeInSeconds: number) => { + const hours = Math.floor(timeInSeconds / 3600); + const minutes = Math.floor((timeInSeconds % 3600) / 60); + const seconds = Math.floor(timeInSeconds % 60); + + if (hours > 0) { + return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + } + return `${minutes}:${seconds.toString().padStart(2, '0')}`; + }; + const togglePlay = () => { if (videoRef.current) { - if (isPlaying) { - videoRef.current.pause(); - } else { - videoRef.current.play(); - } + isPlaying ? videoRef.current.pause() : videoRef.current.play(); setIsPlaying(!isPlaying); } }; const handleProgress = () => { if (videoRef.current) { - const progress = - (videoRef.current.currentTime / videoRef.current.duration) * 100; - setProgress(progress); + setProgress((videoRef.current.currentTime / videoRef.current.duration) * 100); } }; const handleProgressBarClick = (e: React.MouseEvent) => { if (videoRef.current) { const progressBar = e.currentTarget; - const clickPosition = - (e.clientX - progressBar.getBoundingClientRect().left) / - progressBar.offsetWidth; + const clickPosition = (e.clientX - progressBar.getBoundingClientRect().left) / progressBar.offsetWidth; videoRef.current.currentTime = clickPosition * videoRef.current.duration; } }; @@ -87,11 +87,10 @@ export default function NetflixStylePlayer({ const toggleFullScreen = () => { if (!document.fullscreenElement) { containerRef.current?.requestFullscreen(); - setIsFullscreen(true); } else { document.exitFullscreen(); - setIsFullscreen(false); } + setIsFullscreen(!isFullscreen); }; const skipTime = (seconds: number) => { @@ -111,165 +110,56 @@ export default function NetflixStylePlayer({ const toggleMute = () => { if (videoRef.current) { - videoRef.current.muted = !isMuted; - setIsMuted(!isMuted); - if (isMuted) { - videoRef.current.volume = volume; - } else { - setVolume(videoRef.current.volume); - videoRef.current.volume = 0; - } - } - }; - - useEffect(() => { - const handleFullscreenChange = () => { - setIsFullscreen(!!document.fullscreenElement); - }; - document.addEventListener('fullscreenchange', handleFullscreenChange); - return () => { - document.removeEventListener('fullscreenchange', handleFullscreenChange); - }; - }, []); - - const handleVideoClick = (e: React.MouseEvent) => { - // Prevent video click from interfering with button click - e.stopPropagation(); - togglePlay(); - }; - - const handleContainerClick = (e: React.MouseEvent) => { - // Get container dimensions - const container = containerRef.current; - if (!container) return; - - const { left, right, top, bottom, width, height } = - container.getBoundingClientRect(); - - // Define the clickable middle area (e.g., 40% of the width in the middle of the screen) - const middleAreaWidth = width * 0.4; - const middleAreaHeight = height * 0.4; - const centerX = (left + right) / 2; - const centerY = (top + bottom) / 2; - - const clickX = e.clientX; - const clickY = e.clientY; - - // Check if click is within the middle area - const withinX = - clickX >= centerX - middleAreaWidth / 2 && - clickX <= centerX + middleAreaWidth / 2; - const withinY = - clickY >= centerY - middleAreaHeight / 2 && - clickY <= centerY + middleAreaHeight / 2; - - if (withinX && withinY) { - togglePlay(); - setShowControls(true); // Show controls when clicked + const newMuteState = !isMuted; + videoRef.current.muted = newMuteState; + setIsMuted(newMuteState); } }; return ( -
-