From db2423cd0f187b3cf1656635eddadb6cfdeb53a6 Mon Sep 17 00:00:00 2001 From: JohnMark Sill Date: Wed, 16 Feb 2022 22:24:54 -0600 Subject: [PATCH] fix: fix playback button --- .../components/HistoryViewer/HistoryVideo.tsx | 87 +++++++++++-------- .../HistoryViewer/HistoryViewer.tsx | 28 +++--- web/src/components/Timeline/Timeline.tsx | 10 ++- .../components/Timeline/TimelineControls.tsx | 12 ++- web/src/icons/Pause.jsx | 13 +++ 5 files changed, 94 insertions(+), 56 deletions(-) create mode 100644 web/src/icons/Pause.jsx diff --git a/web/src/components/HistoryViewer/HistoryVideo.tsx b/web/src/components/HistoryViewer/HistoryVideo.tsx index f5dcb6fbd..23aadebd9 100644 --- a/web/src/components/HistoryViewer/HistoryVideo.tsx +++ b/web/src/components/HistoryViewer/HistoryVideo.tsx @@ -1,5 +1,5 @@ import { h } from 'preact'; -import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks'; +import { useCallback, useEffect, useRef, useState } from 'preact/hooks'; import { useApiHost } from '../../api'; interface OnTimeUpdateEvent { @@ -66,23 +66,40 @@ export const HistoryVideo = ({ } }, [id, videoHeight]); - const playVideo = (video: HTMLMediaElement) => { - const videoHasNotLoaded = video.readyState <= 1; - if (videoHasNotLoaded) { - video.load(); - } - - video.play().catch((e) => { - console.error('Fail', e); - }); - }; - useEffect(() => { + const playVideo = (video: HTMLMediaElement) => { + console.debug('playVideo: attempt playback'); + video + .play() + .then(() => { + console.debug('playVideo: video started'); + }) + .catch((e) => { + console.error('Fail', { e }); + }); + }; + + const attemptPlayVideo = (video: HTMLMediaElement) => { + const videoHasNotLoaded = video.readyState <= 1; + console.debug('playVideo', { videoHasNotLoaded }); + if (videoHasNotLoaded) { + console.debug('playVideo: attempt to load video'); + video.oncanplay = () => { + console.debug('onLoad: video loaded'); + playVideo(video); + }; + video.load(); + } else { + playVideo(video); + } + }; + const video = videoRef.current; const videoExists = !isNullOrUndefined(video); if (videoExists) { + console.log('check should start', { videoIsPlaying }); if (videoIsPlaying) { - playVideo(video); + attemptPlayVideo(video); } else { video.pause(); } @@ -110,28 +127,24 @@ export const HistoryVideo = ({ [videoIsPlaying] ); - const Video = useCallback(() => { - const videoPropertiesIsUndefined = isNullOrUndefined(videoProperties); - if (videoPropertiesIsUndefined) { - return
; - } - const { posterUrl, videoUrl, height } = videoProperties; - return ( - - ); - }, [videoProperties, videoHeight, videoRef]); - - return