feat(video-player): enhance video player with mute functionality and improved event handling
This commit is contained in:
@ -4,108 +4,128 @@ import { useRef, useEffect } from "react";
|
|||||||
import { usePlaybackStore } from "@/stores/playback-store";
|
import { usePlaybackStore } from "@/stores/playback-store";
|
||||||
|
|
||||||
interface VideoPlayerProps {
|
interface VideoPlayerProps {
|
||||||
src: string;
|
src: string;
|
||||||
poster?: string;
|
poster?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
clipStartTime: number;
|
clipStartTime: number;
|
||||||
trimStart: number;
|
trimStart: number;
|
||||||
trimEnd: number;
|
trimEnd: number;
|
||||||
clipDuration: number;
|
clipDuration: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function VideoPlayer({
|
export function VideoPlayer({
|
||||||
src,
|
src,
|
||||||
poster,
|
poster,
|
||||||
className = "",
|
className = "",
|
||||||
clipStartTime,
|
clipStartTime,
|
||||||
trimStart,
|
trimStart,
|
||||||
trimEnd,
|
trimEnd,
|
||||||
clipDuration
|
clipDuration,
|
||||||
}: VideoPlayerProps) {
|
}: VideoPlayerProps) {
|
||||||
const videoRef = useRef<HTMLVideoElement>(null);
|
const videoRef = useRef<HTMLVideoElement>(null);
|
||||||
const { isPlaying, currentTime, volume, speed } = usePlaybackStore();
|
const { isPlaying, currentTime, volume, speed, muted } = usePlaybackStore();
|
||||||
|
|
||||||
// Calculate if we're within this clip's timeline range
|
// Calculate if we're within this clip's timeline range
|
||||||
const clipEndTime = clipStartTime + (clipDuration - trimStart - trimEnd);
|
const clipEndTime = clipStartTime + (clipDuration - trimStart - trimEnd);
|
||||||
const isInClipRange = currentTime >= clipStartTime && currentTime < clipEndTime;
|
const isInClipRange =
|
||||||
|
currentTime >= clipStartTime && currentTime < clipEndTime;
|
||||||
|
|
||||||
// Sync playback events
|
// Sync playback events
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const video = videoRef.current;
|
const video = videoRef.current;
|
||||||
if (!video || !isInClipRange) return;
|
if (!video || !isInClipRange) return;
|
||||||
|
|
||||||
const handleSeekEvent = (e: CustomEvent) => {
|
const handleSeekEvent = (e: CustomEvent) => {
|
||||||
// Always update video time, even if outside clip range
|
// Always update video time, even if outside clip range
|
||||||
const timelineTime = e.detail.time;
|
const timelineTime = e.detail.time;
|
||||||
const videoTime = Math.max(trimStart, Math.min(
|
const videoTime = Math.max(
|
||||||
clipDuration - trimEnd,
|
trimStart,
|
||||||
timelineTime - clipStartTime + trimStart
|
Math.min(
|
||||||
));
|
clipDuration - trimEnd,
|
||||||
video.currentTime = videoTime;
|
timelineTime - clipStartTime + trimStart
|
||||||
};
|
)
|
||||||
|
);
|
||||||
|
video.currentTime = videoTime;
|
||||||
|
};
|
||||||
|
|
||||||
const handleUpdateEvent = (e: CustomEvent) => {
|
const handleUpdateEvent = (e: CustomEvent) => {
|
||||||
// Always update video time, even if outside clip range
|
// Always update video time, even if outside clip range
|
||||||
const timelineTime = e.detail.time;
|
const timelineTime = e.detail.time;
|
||||||
const targetTime = Math.max(trimStart, Math.min(
|
const targetTime = Math.max(
|
||||||
clipDuration - trimEnd,
|
trimStart,
|
||||||
timelineTime - clipStartTime + trimStart
|
Math.min(
|
||||||
));
|
clipDuration - trimEnd,
|
||||||
|
timelineTime - clipStartTime + trimStart
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if (Math.abs(video.currentTime - targetTime) > 0.5) {
|
if (Math.abs(video.currentTime - targetTime) > 0.5) {
|
||||||
video.currentTime = targetTime;
|
video.currentTime = targetTime;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSpeed = (e: CustomEvent) => {
|
const handleSpeed = (e: CustomEvent) => {
|
||||||
video.playbackRate = e.detail.speed;
|
video.playbackRate = e.detail.speed;
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("playback-seek", handleSeekEvent as EventListener);
|
window.addEventListener("playback-seek", handleSeekEvent as EventListener);
|
||||||
window.addEventListener("playback-update", handleUpdateEvent as EventListener);
|
window.addEventListener(
|
||||||
window.addEventListener("playback-speed", handleSpeed as EventListener);
|
"playback-update",
|
||||||
|
handleUpdateEvent as EventListener
|
||||||
return () => {
|
|
||||||
window.removeEventListener("playback-seek", handleSeekEvent as EventListener);
|
|
||||||
window.removeEventListener("playback-update", handleUpdateEvent as EventListener);
|
|
||||||
window.removeEventListener("playback-speed", handleSpeed as EventListener);
|
|
||||||
};
|
|
||||||
}, [clipStartTime, trimStart, trimEnd, clipDuration, isInClipRange]);
|
|
||||||
|
|
||||||
// Sync playback state
|
|
||||||
useEffect(() => {
|
|
||||||
const video = videoRef.current;
|
|
||||||
if (!video) return;
|
|
||||||
|
|
||||||
if (isPlaying && isInClipRange) {
|
|
||||||
video.play().catch(() => { });
|
|
||||||
} else {
|
|
||||||
video.pause();
|
|
||||||
}
|
|
||||||
}, [isPlaying, isInClipRange]);
|
|
||||||
|
|
||||||
// Sync volume and speed
|
|
||||||
useEffect(() => {
|
|
||||||
const video = videoRef.current;
|
|
||||||
if (!video) return;
|
|
||||||
|
|
||||||
video.volume = volume;
|
|
||||||
video.playbackRate = speed;
|
|
||||||
}, [volume, speed]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<video
|
|
||||||
ref={videoRef}
|
|
||||||
src={src}
|
|
||||||
poster={poster}
|
|
||||||
className={`w-full h-full object-cover ${className}`}
|
|
||||||
playsInline
|
|
||||||
preload="auto"
|
|
||||||
controls={false}
|
|
||||||
disablePictureInPicture
|
|
||||||
disableRemotePlayback
|
|
||||||
style={{ pointerEvents: 'none' }}
|
|
||||||
onContextMenu={(e) => e.preventDefault()}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
}
|
window.addEventListener("playback-speed", handleSpeed as EventListener);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener(
|
||||||
|
"playback-seek",
|
||||||
|
handleSeekEvent as EventListener
|
||||||
|
);
|
||||||
|
window.removeEventListener(
|
||||||
|
"playback-update",
|
||||||
|
handleUpdateEvent as EventListener
|
||||||
|
);
|
||||||
|
window.removeEventListener(
|
||||||
|
"playback-speed",
|
||||||
|
handleSpeed as EventListener
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}, [clipStartTime, trimStart, trimEnd, clipDuration, isInClipRange]);
|
||||||
|
|
||||||
|
// Sync playback state
|
||||||
|
useEffect(() => {
|
||||||
|
const video = videoRef.current;
|
||||||
|
if (!video) return;
|
||||||
|
|
||||||
|
if (isPlaying && isInClipRange) {
|
||||||
|
video.play().catch(() => {});
|
||||||
|
} else {
|
||||||
|
video.pause();
|
||||||
|
}
|
||||||
|
}, [isPlaying, isInClipRange]);
|
||||||
|
|
||||||
|
// Sync volume and speed
|
||||||
|
useEffect(() => {
|
||||||
|
const video = videoRef.current;
|
||||||
|
if (!video) return;
|
||||||
|
|
||||||
|
video.volume = volume;
|
||||||
|
video.muted = muted;
|
||||||
|
video.playbackRate = speed;
|
||||||
|
}, [volume, speed, muted]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<video
|
||||||
|
ref={videoRef}
|
||||||
|
src={src}
|
||||||
|
poster={poster}
|
||||||
|
className={`w-full h-full object-cover ${className}`}
|
||||||
|
playsInline
|
||||||
|
preload="auto"
|
||||||
|
controls={false}
|
||||||
|
disablePictureInPicture
|
||||||
|
disableRemotePlayback
|
||||||
|
style={{ pointerEvents: "none" }}
|
||||||
|
onContextMenu={(e) => e.preventDefault()}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user