2023-01-08 15:37:16 +01:00
|
|
|
import React, { MutableRefObject, useEffect, useState } from "react";
|
|
|
|
import {
|
|
|
|
initialControls,
|
|
|
|
PlayerControls,
|
|
|
|
populateControls,
|
|
|
|
} from "./controlVideo";
|
2023-01-08 17:51:38 +01:00
|
|
|
import { handleBuffered } from "./utils";
|
2023-01-08 15:37:16 +01:00
|
|
|
|
|
|
|
export type PlayerState = {
|
|
|
|
isPlaying: boolean;
|
|
|
|
isPaused: boolean;
|
2023-01-08 16:23:42 +01:00
|
|
|
isSeeking: boolean;
|
|
|
|
isFullscreen: boolean;
|
2023-01-08 17:51:38 +01:00
|
|
|
time: number;
|
|
|
|
duration: number;
|
|
|
|
volume: number;
|
|
|
|
buffered: number;
|
2023-01-08 15:37:16 +01:00
|
|
|
} & PlayerControls;
|
|
|
|
|
2023-01-08 16:23:42 +01:00
|
|
|
export const initialPlayerState: PlayerState = {
|
2023-01-08 15:37:16 +01:00
|
|
|
isPlaying: false,
|
|
|
|
isPaused: true,
|
2023-01-08 16:23:42 +01:00
|
|
|
isFullscreen: false,
|
|
|
|
isSeeking: false,
|
2023-01-08 17:51:38 +01:00
|
|
|
time: 0,
|
|
|
|
duration: 0,
|
|
|
|
volume: 0,
|
|
|
|
buffered: 0,
|
2023-01-08 15:37:16 +01:00
|
|
|
...initialControls,
|
|
|
|
};
|
|
|
|
|
|
|
|
type SetPlayer = (s: React.SetStateAction<PlayerState>) => void;
|
|
|
|
|
|
|
|
function readState(player: HTMLVideoElement, update: SetPlayer) {
|
|
|
|
const state = {
|
|
|
|
...initialPlayerState,
|
|
|
|
};
|
|
|
|
state.isPaused = player.paused;
|
|
|
|
state.isPlaying = !player.paused;
|
2023-01-08 16:23:42 +01:00
|
|
|
state.isFullscreen = !!document.fullscreenElement;
|
|
|
|
state.isSeeking = player.seeking;
|
2023-01-08 17:51:38 +01:00
|
|
|
state.time = player.currentTime;
|
|
|
|
state.duration = player.duration;
|
|
|
|
state.volume = player.volume;
|
|
|
|
state.buffered = handleBuffered(player.currentTime, player.buffered);
|
2023-01-08 15:37:16 +01:00
|
|
|
|
|
|
|
update(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerListeners(player: HTMLVideoElement, update: SetPlayer) {
|
2023-01-08 17:51:38 +01:00
|
|
|
const pause = () => {
|
2023-01-08 15:37:16 +01:00
|
|
|
update((s) => ({ ...s, isPaused: true, isPlaying: false }));
|
2023-01-08 17:51:38 +01:00
|
|
|
};
|
|
|
|
const play = () => {
|
2023-01-08 15:37:16 +01:00
|
|
|
update((s) => ({ ...s, isPaused: false, isPlaying: true }));
|
2023-01-08 17:51:38 +01:00
|
|
|
};
|
|
|
|
const seeking = () => {
|
2023-01-08 16:23:42 +01:00
|
|
|
update((s) => ({ ...s, isSeeking: true }));
|
2023-01-08 17:51:38 +01:00
|
|
|
};
|
|
|
|
const seeked = () => {
|
2023-01-08 16:23:42 +01:00
|
|
|
update((s) => ({ ...s, isSeeking: false }));
|
2023-01-08 17:51:38 +01:00
|
|
|
};
|
|
|
|
const fullscreenchange = () => {
|
2023-01-08 16:23:42 +01:00
|
|
|
update((s) => ({ ...s, isFullscreen: !!document.fullscreenElement }));
|
2023-01-08 17:51:38 +01:00
|
|
|
};
|
|
|
|
const timeupdate = () => {
|
|
|
|
update((s) => ({
|
|
|
|
...s,
|
|
|
|
duration: player.duration,
|
|
|
|
time: player.currentTime,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
const loadedmetadata = () => {
|
|
|
|
update((s) => ({
|
|
|
|
...s,
|
|
|
|
duration: player.duration,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
const volumechange = () => {
|
|
|
|
update((s) => ({
|
|
|
|
...s,
|
|
|
|
volume: player.volume,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
const progress = () => {
|
|
|
|
update((s) => ({
|
|
|
|
...s,
|
|
|
|
buffered: handleBuffered(player.currentTime, player.buffered),
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
player.addEventListener("pause", pause);
|
|
|
|
player.addEventListener("play", play);
|
|
|
|
player.addEventListener("seeking", seeking);
|
|
|
|
player.addEventListener("seeked", seeked);
|
|
|
|
document.addEventListener("fullscreenchange", fullscreenchange);
|
|
|
|
player.addEventListener("timeupdate", timeupdate);
|
|
|
|
player.addEventListener("loadedmetadata", loadedmetadata);
|
|
|
|
player.addEventListener("volumechange", volumechange);
|
|
|
|
player.addEventListener("progress", progress);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
player.removeEventListener("pause", pause);
|
|
|
|
player.removeEventListener("play", play);
|
|
|
|
player.removeEventListener("seeking", seeking);
|
|
|
|
player.removeEventListener("seeked", seeked);
|
|
|
|
document.removeEventListener("fullscreenchange", fullscreenchange);
|
|
|
|
player.removeEventListener("timeupdate", timeupdate);
|
|
|
|
player.removeEventListener("loadedmetadata", loadedmetadata);
|
|
|
|
player.removeEventListener("volumechange", volumechange);
|
|
|
|
player.removeEventListener("progress", progress);
|
|
|
|
};
|
2023-01-08 15:37:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-08 16:23:42 +01:00
|
|
|
export function useVideoPlayer(
|
|
|
|
ref: MutableRefObject<HTMLVideoElement | null>,
|
|
|
|
wrapperRef: MutableRefObject<HTMLDivElement | null>
|
|
|
|
) {
|
2023-01-08 15:37:16 +01:00
|
|
|
const [state, setState] = useState(initialPlayerState);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const player = ref.current;
|
2023-01-08 16:23:42 +01:00
|
|
|
const wrapper = wrapperRef.current;
|
|
|
|
if (player && wrapper) {
|
2023-01-08 15:37:16 +01:00
|
|
|
readState(player, setState);
|
|
|
|
registerListeners(player, setState);
|
2023-01-08 16:23:42 +01:00
|
|
|
setState((s) => ({ ...s, ...populateControls(player, wrapper) }));
|
2023-01-08 15:37:16 +01:00
|
|
|
}
|
2023-01-08 16:23:42 +01:00
|
|
|
}, [ref, wrapperRef]);
|
2023-01-08 15:37:16 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
playerState: state,
|
|
|
|
};
|
|
|
|
}
|