[Video] Fix crash when switching tabs (#4925)
parent
134fcd35d8
commit
99d1a881f2
|
@ -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
|
player.play()
|
||||||
if (isAppFocused === 'active') {
|
} else if (player.playing) {
|
||||||
player.play()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [isScreenFocused, player])
|
}, [isAppFocused, player, isScreenFocused])
|
||||||
|
|
||||||
const enterFullscreen = useCallback(() => {
|
const enterFullscreen = useCallback(() => {
|
||||||
ref.current?.enterFullscreen()
|
ref.current?.enterFullscreen()
|
||||||
|
|
Loading…
Reference in New Issue