[Video] Try/catch video play/pause (#4930)

Co-authored-by: Samuel Newman <10959775+mozzius@users.noreply.github.com>
zio/stable
Samuel Newman 2024-08-13 22:00:03 +01:00 committed by GitHub
parent 57be2ea15b
commit 630ebf523d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import {useIsFocused} from '@react-navigation/native'
import {HITSLOP_30} from '#/lib/constants' import {HITSLOP_30} from '#/lib/constants'
import {useAppState} from '#/lib/hooks/useAppState' import {useAppState} from '#/lib/hooks/useAppState'
import {logger} from '#/logger'
import {useVideoPlayer} from '#/view/com/util/post-embeds/VideoPlayerContext' import {useVideoPlayer} from '#/view/com/util/post-embeds/VideoPlayerContext'
import {android, atoms as a, useTheme} from '#/alf' import {android, atoms as a, useTheme} from '#/alf'
import {Mute_Stroke2_Corner0_Rounded as MuteIcon} from '#/components/icons/Mute' import {Mute_Stroke2_Corner0_Rounded as MuteIcon} from '#/components/icons/Mute'
@ -25,11 +26,18 @@ export function VideoEmbedInnerNative() {
const isAppFocused = useAppState() const isAppFocused = useAppState()
useEffect(() => { useEffect(() => {
try {
if (isAppFocused === 'active' && isScreenFocused && !player.playing) { if (isAppFocused === 'active' && isScreenFocused && !player.playing) {
player.play() player.play()
} else if (player.playing) { } else if (player.playing) {
player.pause() player.pause()
} }
} catch (err) {
logger.error(
'Failed to play/pause while backgrounding/switching screens',
{safeMessage: err},
)
}
}, [isAppFocused, player, isScreenFocused]) }, [isAppFocused, player, isScreenFocused])
const enterFullscreen = useCallback(() => { const enterFullscreen = useCallback(() => {

View File

@ -2,6 +2,8 @@ import React, {useContext} from 'react'
import type {VideoPlayer} from 'expo-video' import type {VideoPlayer} from 'expo-video'
import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video' import {useVideoPlayer as useExpoVideoPlayer} from 'expo-video'
import {logger} from '#/logger'
const VideoPlayerContext = React.createContext<VideoPlayer | null>(null) const VideoPlayerContext = React.createContext<VideoPlayer | null>(null)
export function VideoPlayerProvider({ export function VideoPlayerProvider({
@ -13,9 +15,13 @@ export function VideoPlayerProvider({
}) { }) {
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
const player = useExpoVideoPlayer(source, player => { const player = useExpoVideoPlayer(source, player => {
try {
player.loop = true player.loop = true
player.muted = true player.muted = true
player.play() player.play()
} catch (err) {
logger.error('Failed to init video player', {safeMessage: err})
}
}) })
return ( return (