feat(video-player): enhance video player with mute functionality and improved event handling
This commit is contained in:
@ -20,14 +20,15 @@ export function VideoPlayer({
|
|||||||
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(() => {
|
||||||
@ -37,20 +38,26 @@ export function VideoPlayer({
|
|||||||
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(
|
||||||
|
trimStart,
|
||||||
|
Math.min(
|
||||||
clipDuration - trimEnd,
|
clipDuration - trimEnd,
|
||||||
timelineTime - clipStartTime + trimStart
|
timelineTime - clipStartTime + trimStart
|
||||||
));
|
)
|
||||||
|
);
|
||||||
video.currentTime = videoTime;
|
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(
|
||||||
|
trimStart,
|
||||||
|
Math.min(
|
||||||
clipDuration - trimEnd,
|
clipDuration - trimEnd,
|
||||||
timelineTime - clipStartTime + trimStart
|
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;
|
||||||
@ -62,13 +69,25 @@ export function VideoPlayer({
|
|||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("playback-seek", handleSeekEvent as EventListener);
|
window.addEventListener("playback-seek", handleSeekEvent as EventListener);
|
||||||
window.addEventListener("playback-update", handleUpdateEvent as EventListener);
|
window.addEventListener(
|
||||||
|
"playback-update",
|
||||||
|
handleUpdateEvent as EventListener
|
||||||
|
);
|
||||||
window.addEventListener("playback-speed", handleSpeed as EventListener);
|
window.addEventListener("playback-speed", handleSpeed as EventListener);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("playback-seek", handleSeekEvent as EventListener);
|
window.removeEventListener(
|
||||||
window.removeEventListener("playback-update", handleUpdateEvent as EventListener);
|
"playback-seek",
|
||||||
window.removeEventListener("playback-speed", handleSpeed as EventListener);
|
handleSeekEvent as EventListener
|
||||||
|
);
|
||||||
|
window.removeEventListener(
|
||||||
|
"playback-update",
|
||||||
|
handleUpdateEvent as EventListener
|
||||||
|
);
|
||||||
|
window.removeEventListener(
|
||||||
|
"playback-speed",
|
||||||
|
handleSpeed as EventListener
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}, [clipStartTime, trimStart, trimEnd, clipDuration, isInClipRange]);
|
}, [clipStartTime, trimStart, trimEnd, clipDuration, isInClipRange]);
|
||||||
|
|
||||||
@ -78,7 +97,7 @@ export function VideoPlayer({
|
|||||||
if (!video) return;
|
if (!video) return;
|
||||||
|
|
||||||
if (isPlaying && isInClipRange) {
|
if (isPlaying && isInClipRange) {
|
||||||
video.play().catch(() => { });
|
video.play().catch(() => {});
|
||||||
} else {
|
} else {
|
||||||
video.pause();
|
video.pause();
|
||||||
}
|
}
|
||||||
@ -90,8 +109,9 @@ export function VideoPlayer({
|
|||||||
if (!video) return;
|
if (!video) return;
|
||||||
|
|
||||||
video.volume = volume;
|
video.volume = volume;
|
||||||
|
video.muted = muted;
|
||||||
video.playbackRate = speed;
|
video.playbackRate = speed;
|
||||||
}, [volume, speed]);
|
}, [volume, speed, muted]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<video
|
<video
|
||||||
@ -104,7 +124,7 @@ export function VideoPlayer({
|
|||||||
controls={false}
|
controls={false}
|
||||||
disablePictureInPicture
|
disablePictureInPicture
|
||||||
disableRemotePlayback
|
disableRemotePlayback
|
||||||
style={{ pointerEvents: 'none' }}
|
style={{ pointerEvents: "none" }}
|
||||||
onContextMenu={(e) => e.preventDefault()}
|
onContextMenu={(e) => e.preventDefault()}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user