Skip to content

Commit

Permalink
[Video] Fix crash when switching tabs (#4925)
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok authored Aug 12, 2024
1 parent 134fcd3 commit 99d1a88
Showing 1 changed file with 4 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
player.play()
}
}
}, [isAppFocused, player])

// pause the video when the screen is not focused
useEffect(() => {
if (!isScreenFocused) {
let wasPlaying = player.playing
if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
player.play()
} else if (player.playing) {
player.pause()

return () => {
if (wasPlaying) player.play()
}
}
}, [isScreenFocused, player])
}, [isAppFocused, player, isScreenFocused])

const enterFullscreen = useCallback(() => {
ref.current?.enterFullscreen()
Expand Down

0 comments on commit 99d1a88

Please sign in to comment.