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

54 lines
1.3 KiB
TypeScript
Raw Normal View History

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() {
if (!document.fullscreenEnabled || document.fullscreenElement) return;
wrapper.requestFullscreen();
},
exitFullscreen() {
if (!document.fullscreenElement) return;
document.exitFullscreen();
},
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
};
}