Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: video player fixes (#755)
Ameer Al Ashhabdeb3797586b5
diff --git a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx b/apps/web/app/s/[videoId]/_components/ShareVideo.tsxindex d3d727e5e..b0de17c76 100644--- a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx+++ b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx@@ -308,80 +308,79 @@ export const ShareVideo = forwardRef<};}, []);- const handlePlayPauseClick = async () => {+ const handlePlayPauseClick = async (): Promise<void> => {const videoElement = videoRef.current;-if (!videoElement) return;if (isPlaying) {videoElement.pause();setIsPlaying(false);- } else {- try {- if (!videoReadyToPlay) {- setIsPlaying(true);- } else {- if (!userMuted) {- videoElement.muted = false;- }+ setTempOverlayVisible(true); // Show overlay briefly on pause+ return;+ }++ if (!videoReadyToPlay) {+ console.warn('Video is not ready to play yet.');+ return;+ }- const currentPosition = videoElement.currentTime;+ const currentPosition = videoElement.currentTime;- const playPromise = videoElement.play();+ try {+ videoElement.muted = userMuted;- if (playPromise !== undefined) {- playPromise- .then(() => {- setIsPlaying(true);+ await videoElement.play();+ setIsPlaying(true);+ setTempOverlayVisible(true); // Show overlay briefly on play+ scheduleHideControls(); // Schedule hiding controls after play- if (videoElement.currentTime === 0 && currentPosition > 0) {- videoElement.currentTime = currentPosition;- }- })- .catch((error) => {- console.error("Error with playing:", error);-- if (error.name === "NotAllowedError") {- videoElement.muted = true;- videoElement- .play()- .then(() => {- setIsPlaying(true);- if (!userMuted) {- setTimeout(() => {- videoElement.muted = false;- }, 100);- }-- if (- videoElement.currentTime === 0 &&- currentPosition > 0- ) {- videoElement.currentTime = currentPosition;- }- })- .catch((innerError) => {- console.error(- "Still can't play even with muted:",- innerError- );- });- }- });- } else {- setIsPlaying(true);+ if (videoElement.currentTime === 0 && currentPosition > 0) {+ videoElement.currentTime = currentPosition;+ }- if (videoElement.currentTime === 0 && currentPosition > 0) {- videoElement.currentTime = currentPosition;- }+ if (videoElement.muted !== userMuted) {+ setUserMuted(videoElement.muted);+ }+ } catch (error: unknown) {+ const playError = error as DOMException;+ console.error('Error playing video:', playError);++ if (playError.name === 'NotAllowedError') {+ try {+ videoElement.muted = true;+ await videoElement.play();+ setIsPlaying(true);+ setUserMuted(true);+ setTempOverlayVisible(true);+ scheduleHideControls();++ if (videoElement.currentTime === 0 && currentPosition > 0) {+ videoElement.currentTime = currentPosition;}++ if (!userMuted) {+ requestAnimationFrame(() => {+ try {+ videoElement.muted = false;+ setUserMuted(false);+ } catch (err) {+ console.error('Error unmuting video:', err);+ }+ });+ }+ } catch (mutedError: unknown) {+ console.error('Failed to play video even with mute:', mutedError);+ setIsPlaying(false);+ setTempOverlayVisible(true);}- } catch (error) {- console.error("Error with playing:", error);+ } else {+ setIsPlaying(false);+ setTempOverlayVisible(true);}}};+const applyTimeToVideos = (time: number) => {if (!Number.isFinite(time)) {console.warn("Attempted to set non-finite time:", time);@@ -420,7 +419,9 @@ export const ShareVideo = forwardRef<videoElement.addEventListener("timeupdate", handleTimeUpdate);const handleEnded = () => {- setIsPlaying(false);+ if (videoRef.current && videoRef.current.currentTime === videoRef.current.duration) {+ setIsPlaying(false);+ }};videoElement.addEventListener("ended", handleEnded);@@ -540,123 +541,110 @@ export const ShareVideo = forwardRef<}, [isMP4Source, data, previewWidth, previewHeight, isLargeScreen]);const updatePreviewFrame = (time: number) => {- if (!isLargeScreen) return;+ if (!isLargeScreen || !isMP4Source) return;- if (!isMP4Source) return;setPreviewTime(time);- if (isPreviewSeeking) {- return;- }-- try {- if (scrubbingVideo && previewCanvasRef.current) {- const canvas = previewCanvasRef.current;- const ctx = canvas.getContext("2d");-- if (ctx) {- if (- canvas.width !== previewWidth ||- canvas.height !== previewHeight- ) {- canvas.width = previewWidth;- canvas.height = previewHeight;- }-- setIsPreviewSeeking(true);+ if (isPreviewSeeking) return;- scrubbingVideo.currentTime = time;-- const handleSeeked = () => {- try {- ctx.drawImage(scrubbingVideo, 0, 0, canvas.width, canvas.height);-- ctx.fillStyle = "rgba(0, 0, 0, 0.7)";- ctx.fillRect(0, canvas.height - 20, canvas.width, 20);- ctx.fillStyle = "white";- ctx.font = "12px Arial";- ctx.textAlign = "center";- ctx.fillText(- formatTime(time),- canvas.width / 2,- canvas.height - 6- );+ const drawFrame = () => {+ try {+ if (scrubbingVideo && previewCanvasRef.current) {+ const canvas = previewCanvasRef.current;+ const ctx = canvas.getContext("2d");- setPreviewLoaded(true);- setIsPreviewSeeking(false);- } catch (err) {- console.error("Error drawing frame:", err);- setIsPreviewSeeking(false);+ if (ctx) {+ // Ensure canvas dimensions match preview size+ if (canvas.width !== previewWidth || canvas.height !== previewHeight) {+ canvas.width = previewWidth;+ canvas.height = previewHeight;}- scrubbingVideo.removeEventListener("seeked", handleSeeked);- };-- scrubbingVideo.addEventListener("seeked", handleSeeked);+ setIsPreviewSeeking(true);+ scrubbingVideo.currentTime = time;- const timeoutId = setTimeout(() => {- if (isPreviewSeeking) {+ const handleSeeked = () => {try {- ctx.drawImage(- scrubbingVideo,- 0,- 0,- canvas.width,- canvas.height- );+ // Draw the video frame+ ctx.drawImage(scrubbingVideo, 0, 0, canvas.width, canvas.height);+ // Draw time overlayctx.fillStyle = "rgba(0, 0, 0, 0.7)";ctx.fillRect(0, canvas.height - 20, canvas.width, 20);ctx.fillStyle = "white";ctx.font = "12px Arial";ctx.textAlign = "center";- ctx.fillText(- formatTime(time),- canvas.width / 2,- canvas.height - 6- );+ ctx.fillText(formatTime(time), canvas.width / 2, canvas.height - 6);setPreviewLoaded(true);} catch (err) {- console.error("Error drawing frame after timeout:", err);+ console.error("Error drawing frame:", err);} finally {setIsPreviewSeeking(false);scrubbingVideo.removeEventListener("seeked", handleSeeked);}- }- }, 250);-- return () => clearTimeout(timeoutId);- }- } else if (videoRef.current && previewCanvasRef.current) {- const canvas = previewCanvasRef.current;- const video = videoRef.current;- const ctx = canvas.getContext("2d");+ };++ scrubbingVideo.addEventListener("seeked", handleSeeked);++ // Fallback if seeked event doesn't fire within 250ms+ const timeoutId = setTimeout(() => {+ if (isPreviewSeeking) {+ try {+ ctx.drawImage(scrubbingVideo, 0, 0, canvas.width, canvas.height);+ ctx.fillStyle = "rgba(0, 0, 0, 0.7)";+ ctx.fillRect(0, canvas.height - 20, canvas.width, 20);+ ctx.fillStyle = "white";+ ctx.font = "12px Arial";+ ctx.textAlign = "center";+ ctx.fillText(formatTime(time), canvas.width / 2, canvas.height - 6);+ setPreviewLoaded(true);+ } catch (err) {+ console.error("Error drawing frame after timeout:", err);+ } finally {+ setIsPreviewSeeking(false);+ scrubbingVideo.removeEventListener("seeked", handleSeeked);+ }+ }+ }, 250);- if (ctx) {- try {- canvas.width = previewWidth;- canvas.height = previewHeight;+ return () => clearTimeout(timeoutId);+ }+ } else if (videoRef.current && previewCanvasRef.current) {+ const canvas = previewCanvasRef.current;+ const video = videoRef.current;+ const ctx = canvas.getContext("2d");- ctx.drawImage(video, 0, 0, canvas.width, canvas.height);+ if (ctx) {+ try {+ canvas.width = previewWidth;+ canvas.height = previewHeight;- ctx.fillStyle = "rgba(0, 0, 0, 0.7)";- ctx.fillRect(0, canvas.height - 20, canvas.width, 20);- ctx.fillStyle = "white";- ctx.font = "12px Arial";- ctx.textAlign = "center";- ctx.fillText(formatTime(time), canvas.width / 2, canvas.height - 6);+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);+ ctx.fillStyle = "rgba(0, 0, 0, 0.7)";+ ctx.fillRect(0, canvas.height - 20, canvas.width, 20);+ ctx.fillStyle = "white";+ ctx.font = "12px Arial";+ ctx.textAlign = "center";+ ctx.fillText(formatTime(time), canvas.width / 2, canvas.height - 6);- setPreviewLoaded(true);- } catch (err) {- console.error("Error in fallback video capture:", err);+ setPreviewLoaded(true);+ } catch (err) {+ console.error("Error in fallback video capture:", err);+ }}}+ } catch (err) {+ console.error("Error updating preview frame:", err);+ setIsPreviewSeeking(false);}- } catch (err) {- console.error("Error updating preview frame:", err);- setIsPreviewSeeking(false);- }+ };++ // Schedule the frame drawing with requestAnimationFrame+ const rafId = requestAnimationFrame(drawFrame);++ // Cleanup to cancel any pending animation frame+ return () => cancelAnimationFrame(rafId);};const handleTimelineHover = (@@ -1202,15 +1190,16 @@ export const ShareVideo = forwardRef<{/* Render the main progress bar (white) */}<divclassName="absolute top-2.5 h-1 sm:h-1.5 bg-white z-20"- style={{ width: `${watchedPercentage}%` }}+ style={{ width: `${Math.max(0, Math.min(watchedPercentage + 0.82, 99.15))}%` }}/><divstyle={{boxShadow: "0 0 20px rgba(0,0,0,0.1)",- left: `${watchedPercentage}%`,+ left: `calc(${Math.max(0, Math.min(watchedPercentage + 0.82, 99.15))}% )`,}}className={clsx(- "drag-button absolute top-2 z-20 -mt-1.5 w-5 h-5 bg-white rounded-full cursor-pointer focus:outline-none border-2 border-gray-5",+ "drag-button absolute top-2 z-20 mx-auto -mt-1.5 w-5 h-5 bg-white rounded-full cursor-pointer focus:outline-none border-2 border-gray-5 -translate-x-1/2",+seeking? "scale-125 transition-transform ring-blue-300 ring-offset-2 ring-2": ""