movie-web/src/components/video/hooks/controlVideo.ts

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-01-10 01:01:51 +01:00
import fscreen from "fscreen";
import { canFullscreen, isSafari } from "./fullscreen";
2023-01-08 15:37:16 +01:00
export interface PlayerControls {
play(): void;
pause(): void;
2023-01-08 16:23:42 +01:00
exitFullscreen(): void;
enterFullscreen(): void;
2023-01-08 17:51:38 +01:00
setTime(time: number): void;
setVolume(volume: number): void;
2023-01-08 15:37:16 +01:00
}
export const initialControls: PlayerControls = {
play: () => null,
pause: () => null,
2023-01-08 16:23:42 +01:00
enterFullscreen: () => null,
exitFullscreen: () => null,
2023-01-08 17:51:38 +01:00
setTime: () => null,
setVolume: () => null,
2023-01-08 15:37:16 +01:00
};
2023-01-08 16:23:42 +01:00
export function populateControls(
player: HTMLVideoElement,
wrapper: HTMLDivElement
): PlayerControls {
2023-01-08 15:37:16 +01:00
return {
play() {
player.play();
},
pause() {
player.pause();
},
2023-01-08 16:23:42 +01:00
enterFullscreen() {
2023-01-10 01:01:51 +01:00
if (!canFullscreen || fscreen.fullscreenElement) return;
if (fscreen.fullscreenEnabled) {
fscreen.requestFullscreen(wrapper);
return;
}
if (isSafari) {
(player as any).webkitEnterFullscreen();
}
2023-01-08 16:23:42 +01:00
},
exitFullscreen() {
2023-01-10 01:01:51 +01:00
if (!fscreen.fullscreenElement) return;
fscreen.exitFullscreen();
2023-01-08 16:23:42 +01:00
},
2023-01-08 17:51:38 +01:00
setTime(t) {
// clamp time between 0 and max duration
let time = Math.min(t, player.duration);
time = Math.max(0, time);
// eslint-disable-next-line no-param-reassign
player.currentTime = time;
},
setVolume(v) {
// clamp time between 0 and 1
let volume = Math.min(v, 1);
volume = Math.max(0, volume);
// eslint-disable-next-line no-param-reassign
player.volume = volume;
},
2023-01-08 15:37:16 +01:00
};
}