1- import { useIsFocused } from "@react-navigation/native" ;
21import { useEvent } from "expo" ;
32import { useVideoPlayer , VideoView } from "expo-video" ;
43import { useEffect , useEffectEvent , useMemo , useRef , useState } from "react" ;
5- import { AppState , Pressable , View } from "react-native" ;
4+ import { Pressable , View } from "react-native" ;
65
76import { AppActivityIndicator } from "./ui/AppActivityIndicator" ;
87import { AppText } from "./ui/AppText" ;
98import { SymbolView } from "./ui/AppSymbol" ;
109import { VideoThumbnailImage } from "./VideoThumbnailImage" ;
1110import { useMediaActions } from "../state/mediaActions" ;
1211import { type MediaActionsSource } from "../lib/mediaActionsSource" ;
13- import { useFullscreenVideoOpen , useMediaAudioSessionHold } from "../lib/videoAudioSession " ;
12+ import { useMediaVideoPlayback } from "../lib/useMediaVideoPlayback " ;
1413import { MediaActionsMenu } from "./MediaActionsMenu" ;
1514import { createAudioPlaybackSession } from "../lib/audioSession" ;
1615
@@ -19,13 +18,9 @@ function LoadedMediaVideo(props: {
1918 readonly resolvePlaybackUri ?: ( ) => Promise < string | null > ;
2019 readonly playRequested : boolean ;
2120 readonly paused : boolean ;
21+ readonly visible : boolean ;
2222 readonly onPlaybackStart ?: ( ) => void ;
2323} ) {
24- const focused = useIsFocused ( ) ;
25- const fullscreenVideoOpen = useFullscreenVideoOpen ( ) ;
26- const paused = props . paused || fullscreenVideoOpen ;
27- const active = useRef ( focused && AppState . currentState === "active" ) ;
28- const fullscreen = useRef ( false ) ;
2924 const [ attempt , setAttempt ] = useState ( 0 ) ;
3025 // Expo's Android player also reports completed playback as idle.
3126 const [ loadState , setLoadState ] = useState < "pending" | "complete" | "error" > ( "pending" ) ;
@@ -34,6 +29,8 @@ function LoadedMediaVideo(props: {
3429 player . bufferOptions = { preferredForwardBufferDuration : 5 } ;
3530 } ) ;
3631 const playback = useMemo ( ( ) => createAudioPlaybackSession ( player , true ) , [ player ] ) ;
32+ const { active, fullscreen, createPlayRequest, onFullscreenEnter, onFullscreenExit } =
33+ useMediaVideoPlayback ( player , props , playback ) ;
3734 const previousPlaybackRequested = useRef ( false ) ;
3835 const onPlaybackStart = useEffectEvent ( ( ) => props . onPlaybackStart ?.( ) ) ;
3936 useEffect ( ( ) => {
@@ -64,31 +61,19 @@ function LoadedMediaVideo(props: {
6461 playback . pause ( ) ;
6562 previousPlaybackRequested . current = false ;
6663 } ;
67- } , [ player , playback ] ) ;
64+ } , [ active , fullscreen , player , playback ] ) ;
6865 const { status } = useEvent ( player , "statusChange" , { status : player . status } ) ;
69- const { isPlaying } = useEvent ( player , "playingChange" , { isPlaying : player . playing } ) ;
70- useMediaAudioSessionHold ( isPlaying ) ;
7166 const loadSource = useEffectEvent ( async ( signal : AbortSignal ) => {
67+ const playIfUninterrupted = createPlayRequest ( ) ;
7268 const uri = props . resolvePlaybackUri ? await props . resolvePlaybackUri ( ) : props . uri ;
7369 if ( signal . aborted ) return ;
7470 if ( uri === null ) throw new Error ( "Video unavailable" ) ;
7571 playback . pause ( ) ;
7672 previousPlaybackRequested . current = false ;
7773 await player . replaceAsync ( { uri, contentType : "progressive" } ) ;
78- if ( ! signal . aborted && props . playRequested && active . current ) await playback . play ( ) ;
74+ if ( ! signal . aborted && props . playRequested ) await playIfUninterrupted ( ) ;
7975 } ) ;
8076
81- useEffect ( ( ) => {
82- active . current = focused && ! paused && AppState . currentState === "active" ;
83- if ( ! focused || paused || ( ! active . current && ! fullscreen . current ) ) playback . pause ( ) ;
84- // RN reports both leaving the app and Android fullscreen as background; native handling distinguishes them.
85- const subscription = AppState . addEventListener ( "change" , ( state ) => {
86- active . current = focused && ! paused && state === "active" ;
87- if ( state === "inactive" || ( state === "background" && ! fullscreen . current ) ) playback . pause ( ) ;
88- } ) ;
89- return ( ) => subscription . remove ( ) ;
90- } , [ focused , playback , paused ] ) ;
91-
9277 useEffect ( ( ) => {
9378 const controller = new AbortController ( ) ;
9479 // oxlint-disable-next-line react/set-state-in-effect -- Starting a native media load publishes pending before its async result.
@@ -115,12 +100,8 @@ function LoadedMediaVideo(props: {
115100 nativeControls
116101 contentFit = "contain"
117102 fullscreenOptions = { { enable : true } }
118- onFullscreenEnter = { ( ) => {
119- fullscreen . current = true ;
120- } }
121- onFullscreenExit = { ( ) => {
122- fullscreen . current = false ;
123- } }
103+ onFullscreenEnter = { onFullscreenEnter }
104+ onFullscreenExit = { onFullscreenExit }
124105 allowsPictureInPicture = { false }
125106 />
126107 { loadState === "error" || ( loadState === "complete" && status === "error" ) ? (
@@ -150,6 +131,7 @@ interface MediaVideoPlayerProps {
150131 readonly name : string ;
151132 readonly thumbnailKey : string ;
152133 readonly thumbnailVisible ?: boolean ;
134+ readonly visible ?: boolean ;
153135 readonly unavailable ?: boolean ;
154136 readonly expanded ?: boolean ;
155137 readonly autoPlay ?: boolean ;
@@ -176,6 +158,7 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
176158 resolvePlaybackUri = { props . resolvePlaybackUri }
177159 playRequested = { ! props . expanded || props . autoPlay === true }
178160 paused = { ( props . paused ?? false ) || mediaActions . sharing }
161+ visible = { props . visible ?? true }
179162 onPlaybackStart = { props . onPlaybackStart }
180163 />
181164 ) : (
0 commit comments