Skip to content

Move keyboard controls to video controls #10617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions web/src/components/player/HlsVideoPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
MutableRefObject,
ReactNode,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import Hls from "hls.js";
import { isDesktop, isMobile } from "react-device-detect";
import useKeyboardListener from "@/hooks/use-keyboard-listener";
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
import VideoControls from "./VideoControls";

Expand Down Expand Up @@ -87,56 +85,6 @@ export default function HlsVideoPlayer({
const [controls, setControls] = useState(isMobile);
const [controlsOpen, setControlsOpen] = useState(false);

const onKeyboardShortcut = useCallback(
(key: string, down: boolean, repeat: boolean) => {
if (!videoRef.current) {
return;
}

switch (key) {
case "ArrowLeft":
if (down) {
const currentTime = videoRef.current.currentTime;

if (currentTime) {
videoRef.current.currentTime = Math.max(0, currentTime - 5);
}
}
break;
case "ArrowRight":
if (down) {
const currentTime = videoRef.current.currentTime;

if (currentTime) {
videoRef.current.currentTime = currentTime + 5;
}
}
break;
case "m":
if (down && !repeat && videoRef.current) {
videoRef.current.muted = !videoRef.current.muted;
}
break;
case " ":
if (down && videoRef.current) {
if (videoRef.current.paused) {
videoRef.current.play();
} else {
videoRef.current.pause();
}
}
break;
}
},
// only update when preview only changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[videoRef.current],
);
useKeyboardListener(
["ArrowLeft", "ArrowRight", "m", " "],
onKeyboardShortcut,
);

return (
<div
className={`relative ${visible ? "visible" : "hidden"}`}
Expand Down
35 changes: 35 additions & 0 deletions web/src/components/player/VideoControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MdVolumeUp,
} from "react-icons/md";
import { Slider } from "../ui/slider-volume";
import useKeyboardListener from "@/hooks/use-keyboard-listener";

type VideoControls = {
volume?: boolean;
Expand Down Expand Up @@ -100,6 +101,40 @@ export default function VideoControls({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [video?.volume, video?.muted]);

const onKeyboardShortcut = useCallback(
(key: string, down: boolean, repeat: boolean) => {
switch (key) {
case "ArrowLeft":
if (down) {
onSeek(-10);
}
break;
case "ArrowRight":
if (down) {
onSeek(10);
}
break;
case "m":
if (down && !repeat && video) {
video.muted = !video.muted;
}
break;
case " ":
if (down) {
onPlayPause(!isPlaying);
}
break;
}
},
// only update when preview only changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[video, isPlaying, onSeek],
);
useKeyboardListener(
["ArrowLeft", "ArrowRight", "m", " "],
onKeyboardShortcut,
);

if (!show) {
return;
}
Expand Down