[Video] Fix crash when switching tabs (#4925)

zio/stable
Hailey 2024-08-12 16:49:17 -07:00 committed by GitHub
parent 134fcd35d8
commit 99d1a881f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 19 deletions

View File

@ -23,29 +23,14 @@ export function VideoEmbedInnerNative() {
const ref = useRef<VideoView>(null) const ref = useRef<VideoView>(null)
const isScreenFocused = useIsFocused() const isScreenFocused = useIsFocused()
const isAppFocused = useAppState() const isAppFocused = useAppState()
const prevFocusedRef = useRef(isAppFocused)
// resume video when coming back from background
useEffect(() => { useEffect(() => {
if (isAppFocused !== prevFocusedRef.current) { if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
prevFocusedRef.current = isAppFocused
if (isAppFocused === 'active') {
player.play() player.play()
} } else if (player.playing) {
}
}, [isAppFocused, player])
// pause the video when the screen is not focused
useEffect(() => {
if (!isScreenFocused) {
let wasPlaying = player.playing
player.pause() player.pause()
return () => {
if (wasPlaying) player.play()
} }
} }, [isAppFocused, player, isScreenFocused])
}, [isScreenFocused, player])
const enterFullscreen = useCallback(() => { const enterFullscreen = useCallback(() => {
ref.current?.enterFullscreen() ref.current?.enterFullscreen()