11import { useIsFocused } from "@react-navigation/native" ;
22import { useEvent } from "expo" ;
33import { useVideoPlayer , VideoView } from "expo-video" ;
4- import { useEffect , useEffectEvent , useRef , useState } from "react" ;
4+ import { useEffect , useEffectEvent , useMemo , useRef , useState } from "react" ;
55import { AppState , Pressable , View } from "react-native" ;
66
77import { AppActivityIndicator } from "./ui/AppActivityIndicator" ;
@@ -12,12 +12,14 @@ import { useMediaActions } from "../state/mediaActions";
1212import { type MediaActionsSource } from "../lib/mediaActionsSource" ;
1313import { useFullscreenVideoOpen , useMediaAudioSessionHold } from "../lib/videoAudioSession" ;
1414import { MediaActionsMenu } from "./MediaActionsMenu" ;
15+ import { createAudioPlaybackSession } from "../lib/audioSession" ;
1516
1617function LoadedMediaVideo ( props : {
1718 readonly uri : string ;
1819 readonly resolvePlaybackUri ?: ( ) => Promise < string | null > ;
1920 readonly playRequested : boolean ;
2021 readonly paused : boolean ;
22+ readonly onPlaybackStart ?: ( ) => void ;
2123} ) {
2224 const focused = useIsFocused ( ) ;
2325 const fullscreenVideoOpen = useFullscreenVideoOpen ( ) ;
@@ -31,28 +33,61 @@ function LoadedMediaVideo(props: {
3133 player . staysActiveInBackground = false ;
3234 player . bufferOptions = { preferredForwardBufferDuration : 5 } ;
3335 } ) ;
36+ const playback = useMemo ( ( ) => createAudioPlaybackSession ( player , true ) , [ player ] ) ;
37+ const previousPlaybackRequested = useRef ( false ) ;
38+ const onPlaybackStart = useEffectEvent ( ( ) => props . onPlaybackStart ?.( ) ) ;
39+ useEffect ( ( ) => {
40+ const subscription = player . addListener ( "playingChange" , ( { isPlaying } ) => {
41+ if ( isPlaying && ! active . current && ! fullscreen . current ) playback . pause ( ) ;
42+ else playback . playingChanged ( isPlaying ) ;
43+ } ) ;
44+ const requested = player . addListener ( "playbackRequestedChange" , ( { isPlaybackRequested } ) => {
45+ const wasPlaybackRequested = previousPlaybackRequested . current ;
46+ previousPlaybackRequested . current = isPlaybackRequested ;
47+ if ( ! isPlaybackRequested ) {
48+ if ( playback . requested ) playback . pause ( ) ;
49+ } else if ( ! active . current && ! fullscreen . current ) playback . pause ( ) ;
50+ else {
51+ if ( ! wasPlaybackRequested ) onPlaybackStart ( ) ;
52+ playback . playingChanged ( true ) ;
53+ }
54+ } ) ;
55+ const ended = player . addListener ( "playToEnd" , ( ) => playback . pause ( ) ) ;
56+ const failed = player . addListener ( "statusChange" , ( { status } ) => {
57+ if ( status === "error" ) playback . pause ( ) ;
58+ } ) ;
59+ return ( ) => {
60+ subscription . remove ( ) ;
61+ requested . remove ( ) ;
62+ ended . remove ( ) ;
63+ failed . remove ( ) ;
64+ playback . pause ( ) ;
65+ previousPlaybackRequested . current = false ;
66+ } ;
67+ } , [ player , playback ] ) ;
3468 const { status } = useEvent ( player , "statusChange" , { status : player . status } ) ;
3569 const { isPlaying } = useEvent ( player , "playingChange" , { isPlaying : player . playing } ) ;
3670 useMediaAudioSessionHold ( isPlaying ) ;
3771 const loadSource = useEffectEvent ( async ( signal : AbortSignal ) => {
3872 const uri = props . resolvePlaybackUri ? await props . resolvePlaybackUri ( ) : props . uri ;
3973 if ( signal . aborted ) return ;
4074 if ( uri === null ) throw new Error ( "Video unavailable" ) ;
41- player . pause ( ) ;
75+ playback . pause ( ) ;
76+ previousPlaybackRequested . current = false ;
4277 await player . replaceAsync ( { uri, contentType : "progressive" } ) ;
43- if ( ! signal . aborted && props . playRequested && active . current ) player . play ( ) ;
78+ if ( ! signal . aborted && props . playRequested && active . current ) await playback . play ( ) ;
4479 } ) ;
4580
4681 useEffect ( ( ) => {
4782 active . current = focused && ! paused && AppState . currentState === "active" ;
48- if ( ! focused || paused || ( ! active . current && ! fullscreen . current ) ) player . pause ( ) ;
83+ if ( ! focused || paused || ( ! active . current && ! fullscreen . current ) ) playback . pause ( ) ;
4984 // RN reports both leaving the app and Android fullscreen as background; native handling distinguishes them.
5085 const subscription = AppState . addEventListener ( "change" , ( state ) => {
5186 active . current = focused && ! paused && state === "active" ;
52- if ( state === "inactive" || ( state === "background" && ! fullscreen . current ) ) player . pause ( ) ;
87+ if ( state === "inactive" || ( state === "background" && ! fullscreen . current ) ) playback . pause ( ) ;
5388 } ) ;
5489 return ( ) => subscription . remove ( ) ;
55- } , [ focused , player , paused ] ) ;
90+ } , [ focused , playback , paused ] ) ;
5691
5792 useEffect ( ( ) => {
5893 const controller = new AbortController ( ) ;
@@ -117,6 +152,7 @@ interface MediaVideoPlayerProps {
117152 readonly expanded ?: boolean ;
118153 readonly autoPlay ?: boolean ;
119154 readonly paused ?: boolean ;
155+ readonly onPlaybackStart ?: ( ) => void ;
120156 readonly actionsSource ?: MediaActionsSource ;
121157}
122158
@@ -138,6 +174,7 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
138174 resolvePlaybackUri = { props . resolvePlaybackUri }
139175 playRequested = { ! props . expanded || props . autoPlay === true }
140176 paused = { ( props . paused ?? false ) || mediaActions . sharing }
177+ onPlaybackStart = { props . onPlaybackStart }
141178 />
142179 ) : (
143180 < MediaActionsMenu media = { mediaActions } inModal = { props . expanded } style = { { flex : 1 } } >
@@ -150,7 +187,10 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
150187 accessibilityState = { { disabled : props . uri === null || props . unavailable === true } }
151188 // Stays pressable so the long-press menu still opens on a failed or unsigned tile.
152189 onPress = { ( ) => {
153- if ( props . uri !== null && ! props . unavailable ) setPlaybackUri ( props . uri ) ;
190+ if ( props . uri !== null && ! props . unavailable ) {
191+ props . onPlaybackStart ?.( ) ;
192+ setPlaybackUri ( props . uri ) ;
193+ }
154194 } }
155195 className = "flex-1 items-center justify-center px-4"
156196 >
0 commit comments