[Video] Try/catch video play/pause (#4930)
Co-authored-by: Samuel Newman <10959775+mozzius@users.noreply.github.com>zio/stable
parent
57be2ea15b
commit
630ebf523d
|
@ -8,6 +8,7 @@ import {useIsFocused} from '@react-navigation/native'
|
|||
|
||||
import {HITSLOP_30} from '#/lib/constants'
|
||||
import {useAppState} from '#/lib/hooks/useAppState'
|
||||
import {logger} from '#/logger'
|
||||
import {useVideoPlayer} from '#/view/com/util/post-embeds/VideoPlayerContext'
|
||||
import {android, atoms as a, useTheme} from '#/alf'
|
||||
import {Mute_Stroke2_Corner0_Rounded as MuteIcon} from '#/components/icons/Mute'
|
||||
|
@ -25,10 +26,17 @@ export function VideoEmbedInnerNative() {
|
|||
const isAppFocused = useAppState()
|
||||
|
||||
useEffect(() => {
|
||||
if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
|
||||
player.play()
|
||||
} else if (player.playing) {
|
||||
player.pause()
|
||||
try {
|
||||
if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
|
||||
player.play()
|
||||
} else if (player.playing) {
|
||||
player.pause()
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
'Failed to play/pause while backgrounding/switching screens',
|
||||
{safeMessage: err},
|
||||
)
|
||||
}
|
||||
}, [isAppFocused, player, isScreenFocused])
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@ import React, {useContext} from 'react'
|
|||
import type {VideoPlayer} from 'expo-video'
|
||||
import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video'
|
||||
|
||||
import {logger} from '#/logger'
|
||||
|
||||
const VideoPlayerContext = React.createContext<VideoPlayer | null>(null)
|
||||
|
||||
export function VideoPlayerProvider({
|
||||
|
@ -13,9 +15,13 @@ export function VideoPlayerProvider({
|
|||
}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
const player = useExpoVideoPlayer(source, player => {
|
||||
player.loop = true
|
||||
player.muted = true
|
||||
player.play()
|
||||
try {
|
||||
player.loop = true
|
||||
player.muted = true
|
||||
player.play()
|
||||
} catch (err) {
|
||||
logger.error('Failed to init video player', {safeMessage: err})
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
|
|
Loading…
Reference in New Issue