@@ -4,6 +4,7 @@ import type {View} from 'react-native';
4
4
import type { VideoWithOnFullScreenUpdate } from '@components/VideoPlayer/types' ;
5
5
import useCurrentReportID from '@hooks/useCurrentReportID' ;
6
6
import usePrevious from '@hooks/usePrevious' ;
7
+ import Visibility from '@libs/Visibility' ;
7
8
import type ChildrenProps from '@src/types/utils/ChildrenProps' ;
8
9
import type { PlaybackContext , StatusCallback } from './types' ;
9
10
@@ -19,6 +20,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
19
20
const prevCurrentReportID = usePrevious ( currentReportID ) ;
20
21
const videoResumeTryNumberRef = useRef < number > ( 0 ) ;
21
22
const playVideoPromiseRef = useRef < Promise < AVPlaybackStatus > > ( ) ;
23
+ const isPlayPendingRef = useRef ( false ) ;
22
24
23
25
const pauseVideo = useCallback ( ( ) => {
24
26
currentVideoPlayerRef . current ?. setStatusAsync ?.( { shouldPlay : false } ) ;
@@ -29,6 +31,10 @@ function PlaybackContextProvider({children}: ChildrenProps) {
29
31
} , [ currentVideoPlayerRef ] ) ;
30
32
31
33
const playVideo = useCallback ( ( ) => {
34
+ if ( ! Visibility . isVisible ( ) ) {
35
+ isPlayPendingRef . current = true ;
36
+ return ;
37
+ }
32
38
currentVideoPlayerRef . current ?. getStatusAsync ?.( ) . then ( ( status ) => {
33
39
const newStatus : AVPlaybackStatusToSet = { shouldPlay : true } ;
34
40
if ( 'durationMillis' in status && status . durationMillis === status . positionMillis ) {
@@ -94,12 +100,23 @@ function PlaybackContextProvider({children}: ChildrenProps) {
94
100
// This prevents the video that plays when the app opens from being interrupted when currentReportID
95
101
// is initially empty or '-1', or when it changes from empty/'-1' to another value
96
102
// after the report screen in the central pane is mounted on the large screen.
97
- if ( ! currentReportID || ! prevCurrentReportID || currentReportID === '-1' || prevCurrentReportID === '-1' ) {
103
+ if ( ! currentReportID || ! prevCurrentReportID || currentReportID === '-1' || prevCurrentReportID === '-1' || currentReportID === prevCurrentReportID ) {
98
104
return ;
99
105
}
100
106
resetVideoPlayerData ( ) ;
101
107
} , [ currentReportID , prevCurrentReportID , resetVideoPlayerData ] ) ;
102
108
109
+ useEffect ( ( ) => {
110
+ const unsubscribeVisibilityListener = Visibility . onVisibilityChange ( ( ) => {
111
+ if ( ! Visibility . isVisible ( ) || ! isPlayPendingRef . current ) {
112
+ return ;
113
+ }
114
+ playVideo ( ) ;
115
+ isPlayPendingRef . current = false ;
116
+ } ) ;
117
+ return unsubscribeVisibilityListener ;
118
+ } , [ playVideo ] ) ;
119
+
103
120
const contextValue = useMemo (
104
121
( ) => ( {
105
122
updateCurrentlyPlayingURL,
0 commit comments