Optimized Video: Fix exception on pause (#2109)
This commit is contained in:
parent
abd92f2ae8
commit
b69d4ee2f1
@ -1,33 +1,31 @@
|
|||||||
import { useCallback, useEffect, useRef } from '../../../../lib/teact/teact';
|
import { useCallback, useEffect, useRef } from '../../../../lib/teact/teact';
|
||||||
|
|
||||||
import { fastRaf } from '../../../../util/schedulers';
|
import { fastRaf } from '../../../../util/schedulers';
|
||||||
import safePlay from '../../../../util/safePlay';
|
|
||||||
import useBackgroundMode from '../../../../hooks/useBackgroundMode';
|
import useBackgroundMode from '../../../../hooks/useBackgroundMode';
|
||||||
import useHeavyAnimationCheck from '../../../../hooks/useHeavyAnimationCheck';
|
import useHeavyAnimationCheck from '../../../../hooks/useHeavyAnimationCheck';
|
||||||
|
import usePlayPause from '../../../../hooks/usePlayPause';
|
||||||
|
|
||||||
export default function useVideoAutoPause(playerRef: { current: HTMLVideoElement | null }, canPlay: boolean) {
|
export default function useVideoAutoPause(playerRef: { current: HTMLVideoElement | null }, canPlay: boolean) {
|
||||||
const canPlayRef = useRef();
|
const canPlayRef = useRef();
|
||||||
canPlayRef.current = canPlay;
|
canPlayRef.current = canPlay;
|
||||||
|
|
||||||
|
const { play, pause } = usePlayPause(playerRef);
|
||||||
|
|
||||||
const isFrozenRef = useRef();
|
const isFrozenRef = useRef();
|
||||||
|
|
||||||
const freezePlaying = useCallback(() => {
|
const freezePlaying = useCallback(() => {
|
||||||
isFrozenRef.current = true;
|
isFrozenRef.current = true;
|
||||||
|
|
||||||
playerRef.current?.pause();
|
pause();
|
||||||
}, [playerRef]);
|
}, [pause]);
|
||||||
|
|
||||||
const unfreezePlaying = useCallback(() => {
|
const unfreezePlaying = useCallback(() => {
|
||||||
isFrozenRef.current = false;
|
isFrozenRef.current = false;
|
||||||
|
|
||||||
if (
|
if (canPlayRef.current) {
|
||||||
playerRef.current && canPlayRef.current
|
play();
|
||||||
// At this point `HTMLVideoElement` can be unmounted from the DOM
|
|
||||||
&& document.body.contains(playerRef.current)
|
|
||||||
) {
|
|
||||||
safePlay(playerRef.current);
|
|
||||||
}
|
}
|
||||||
}, [playerRef]);
|
}, [play]);
|
||||||
|
|
||||||
const unfreezePlayingOnRaf = useCallback(() => {
|
const unfreezePlayingOnRaf = useCallback(() => {
|
||||||
fastRaf(unfreezePlaying);
|
fastRaf(unfreezePlaying);
|
||||||
@ -38,23 +36,19 @@ export default function useVideoAutoPause(playerRef: { current: HTMLVideoElement
|
|||||||
|
|
||||||
const handlePlaying = useCallback(() => {
|
const handlePlaying = useCallback(() => {
|
||||||
if (!canPlayRef.current || isFrozenRef.current) {
|
if (!canPlayRef.current || isFrozenRef.current) {
|
||||||
playerRef.current!.pause();
|
pause();
|
||||||
}
|
}
|
||||||
}, [playerRef]);
|
}, [pause]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!playerRef.current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (canPlay) {
|
if (canPlay) {
|
||||||
if (!isFrozenRef.current) {
|
if (!isFrozenRef.current) {
|
||||||
safePlay(playerRef.current);
|
play();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
playerRef.current!.pause();
|
pause();
|
||||||
}
|
}
|
||||||
}, [canPlay, playerRef]);
|
}, [canPlay, play, pause]);
|
||||||
|
|
||||||
return { handlePlaying };
|
return { handlePlaying };
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/hooks/usePlayPause.ts
Normal file
33
src/hooks/usePlayPause.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import { useCallback, useRef } from '../lib/teact/teact';
|
||||||
|
|
||||||
|
export default function usePlayPause(mediaRef: React.RefObject<HTMLMediaElement>) {
|
||||||
|
const shouldPauseRef = useRef(false);
|
||||||
|
const isLoadingPlayRef = useRef(false);
|
||||||
|
|
||||||
|
const play = useCallback(() => {
|
||||||
|
shouldPauseRef.current = false;
|
||||||
|
if (mediaRef.current && !isLoadingPlayRef.current && document.body.contains(mediaRef.current)) {
|
||||||
|
isLoadingPlayRef.current = true;
|
||||||
|
mediaRef.current.play().then(() => {
|
||||||
|
isLoadingPlayRef.current = false;
|
||||||
|
if (shouldPauseRef.current) {
|
||||||
|
mediaRef.current?.pause();
|
||||||
|
shouldPauseRef.current = false;
|
||||||
|
}
|
||||||
|
}).catch((e) => {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.warn(e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [mediaRef]);
|
||||||
|
|
||||||
|
const pause = useCallback(() => {
|
||||||
|
if (isLoadingPlayRef.current) {
|
||||||
|
shouldPauseRef.current = true;
|
||||||
|
} else {
|
||||||
|
mediaRef.current?.pause();
|
||||||
|
}
|
||||||
|
}, [mediaRef]);
|
||||||
|
|
||||||
|
return { play, pause };
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user