[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 isScreenFocused = useIsFocused()
const isAppFocused = useAppState()
const prevFocusedRef = useRef(isAppFocused)
// resume video when coming back from background
useEffect(() => {
if (isAppFocused !== prevFocusedRef.current) {
prevFocusedRef.current = isAppFocused
if (isAppFocused === 'active') {
if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
player.play()
}
}
}, [isAppFocused, player])
// pause the video when the screen is not focused
useEffect(() => {
if (!isScreenFocused) {
let wasPlaying = player.playing
} else if (player.playing) {
player.pause()
return () => {
if (wasPlaying) player.play()
}
}
}, [isScreenFocused, player])
}, [isAppFocused, player, isScreenFocused])
const enterFullscreen = useCallback(() => {
ref.current?.enterFullscreen()