From e569f15661957dc0a5b347ce4e69629a054d878e Mon Sep 17 00:00:00 2001 From: Jelle van Snik Date: Sun, 12 Feb 2023 13:16:18 +0100 Subject: [PATCH] fix restoring of time when changing source --- .../controllers/ProgressListenerController.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/video/components/controllers/ProgressListenerController.tsx b/src/video/components/controllers/ProgressListenerController.tsx index 27434bd5..2739f412 100644 --- a/src/video/components/controllers/ProgressListenerController.tsx +++ b/src/video/components/controllers/ProgressListenerController.tsx @@ -16,10 +16,15 @@ export function ProgressListenerController(props: Props) { const progress = useProgress(descriptor); const controls = useControls(descriptor); const didInitialize = useRef(null); + const lastTime = useRef(props.startAt ?? 0); // time updates (throttled) const updateTime = useMemo( - () => throttle((a: number, b: number) => props.onProgress?.(a, b), 1000), + () => + throttle((a: number, b: number) => { + lastTime.current = a; + props.onProgress?.(a, b); + }, 1000), [props] ); useEffect(() => { @@ -37,11 +42,16 @@ export function ProgressListenerController(props: Props) { useEffect(() => { if (didInitialize.current) return; if (mediaPlaying.isFirstLoading || Number.isNaN(progress.duration)) return; - if (props.startAt !== undefined) { - controls.setTime(props.startAt); - } + controls.setTime(lastTime.current); didInitialize.current = true; }, [didInitialize, props, progress, mediaPlaying, controls]); + useEffect(() => { + // if it initialized, but media starts loading for the first time again. + // reset initalized so it will restore time again + if (didInitialize.current && mediaPlaying.isFirstLoading) + didInitialize.current = null; + }, [mediaPlaying]); + return null; }