improvement: better play/pause

This commit is contained in:
JohnMark Sill 2022-06-24 10:52:17 -05:00 committed by Blake Blackshear
parent 43f05c18d6
commit a67a768e89
3 changed files with 50 additions and 22 deletions

13
web/package-lock.json generated
View File

@ -30,6 +30,7 @@
"@testing-library/preact": "^2.0.1", "@testing-library/preact": "^2.0.1",
"@testing-library/preact-hooks": "^1.1.0", "@testing-library/preact-hooks": "^1.1.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"@types/video.js": "^7.3.42",
"@typescript-eslint/eslint-plugin": "^5.18.0", "@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0", "@typescript-eslint/parser": "^5.18.0",
"autoprefixer": "^10.4.2", "autoprefixer": "^10.4.2",
@ -3234,6 +3235,12 @@
"@types/jest": "*" "@types/jest": "*"
} }
}, },
"node_modules/@types/video.js": {
"version": "7.3.42",
"resolved": "https://registry.npmjs.org/@types/video.js/-/video.js-7.3.42.tgz",
"integrity": "sha512-AD6AQNMgLTqrgoayC6SshKh8EDkDd9x5pmEuiY9YsniHlhn5jPXdkVqrzKLwviapaRhQF15TQYxo1JWpqXCUBg==",
"dev": true
},
"node_modules/@types/yargs": { "node_modules/@types/yargs": {
"version": "16.0.4", "version": "16.0.4",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz",
@ -14504,6 +14511,12 @@
"@types/jest": "*" "@types/jest": "*"
} }
}, },
"@types/video.js": {
"version": "7.3.42",
"resolved": "https://registry.npmjs.org/@types/video.js/-/video.js-7.3.42.tgz",
"integrity": "sha512-AD6AQNMgLTqrgoayC6SshKh8EDkDd9x5pmEuiY9YsniHlhn5jPXdkVqrzKLwviapaRhQF15TQYxo1JWpqXCUBg==",
"dev": true
},
"@types/yargs": { "@types/yargs": {
"version": "16.0.4", "version": "16.0.4",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz",

View File

@ -32,6 +32,7 @@
"@testing-library/preact": "^2.0.1", "@testing-library/preact": "^2.0.1",
"@testing-library/preact-hooks": "^1.1.0", "@testing-library/preact-hooks": "^1.1.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"@types/video.js": "^7.3.42",
"@typescript-eslint/eslint-plugin": "^5.18.0", "@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0", "@typescript-eslint/parser": "^5.18.0",
"autoprefixer": "^10.4.2", "autoprefixer": "^10.4.2",

View File

@ -1,5 +1,5 @@
import { h } from 'preact'; import { h } from 'preact';
import { useCallback, useEffect, useRef } from 'preact/hooks'; import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
import { useApiHost } from '../../api'; import { useApiHost } from '../../api';
import { isNullOrUndefined } from '../../utils/objectUtils'; import { isNullOrUndefined } from '../../utils/objectUtils';
@ -7,7 +7,7 @@ import 'videojs-seek-buttons';
import 'video.js/dist/video-js.css'; import 'video.js/dist/video-js.css';
import 'videojs-seek-buttons/dist/videojs-seek-buttons.css'; import 'videojs-seek-buttons/dist/videojs-seek-buttons.css';
import videojs from 'video.js'; import videojs, { VideoJsPlayer } from 'video.js';
interface OnTimeUpdateEvent { interface OnTimeUpdateEvent {
timestamp: number; timestamp: number;
@ -34,18 +34,28 @@ export const HistoryVideo = ({
const apiHost = useApiHost(); const apiHost = useApiHost();
const videoRef = useRef<HTMLVideoElement>(null); const videoRef = useRef<HTMLVideoElement>(null);
const [video, setVideo] = useState<VideoJsPlayer>();
useEffect(() => { useEffect(() => {
let video: any; let video: VideoJsPlayer
if (videoRef.current && id) { if (videoRef.current) {
video = videojs(videoRef.current, {}); video = videojs(videoRef.current, {})
setVideo(video)
} }
() => video?.dispose()
}, [videoRef]); }, [videoRef]);
useEffect(() => { useEffect(() => {
if (!id) { if (!video) {
return; return
} }
const video = videojs(videoRef.current);
if (!id) {
video.pause()
return
}
video.src({ video.src({
src: `${apiHost}/vod/event/${id}/index.m3u8`, src: `${apiHost}/vod/event/${id}/index.m3u8`,
type: 'application/vnd.apple.mpegurl', type: 'application/vnd.apple.mpegurl',
@ -54,20 +64,7 @@ export const HistoryVideo = ({
if (videoIsPlaying) { if (videoIsPlaying) {
video.play(); video.play();
} }
}, [id]); }, [video, id]);
useEffect(() => {
if (!videoRef) {
return;
}
const video = videojs(videoRef.current);
if (video.paused() && videoIsPlaying) {
video.play();
} else if (!video.paused() && !videoIsPlaying) {
video.pause();
}
}, [videoIsPlaying, videoRef]);
useEffect(() => { useEffect(() => {
const video = videoRef.current; const video = videoRef.current;
@ -90,11 +87,28 @@ export const HistoryVideo = ({
[videoIsPlaying, onTimeUpdate] [videoIsPlaying, onTimeUpdate]
); );
useEffect(() => {
if (video && video.readyState() >= 1) {
if (videoIsPlaying) {
video.play()
} else {
video.pause()
}
}
}, [video, videoIsPlaying])
const onLoad = useCallback(() => {
if (video && video.readyState() >= 1 && videoIsPlaying) {
video.play()
}
}, [video, videoIsPlaying])
return ( return (
<div data-vjs-player> <div data-vjs-player>
<video <video
ref={videoRef} ref={videoRef}
onTimeUpdate={onTimeUpdateHandler} onTimeUpdate={onTimeUpdateHandler}
onLoadedMetadata={onLoad}
onPause={onPause} onPause={onPause}
onPlay={onPlay} onPlay={onPlay}
className="video-js vjs-fluid" className="video-js vjs-fluid"