Skip to content

Commit 9819f37

Browse files
authored
Merge pull request #47217 from bernhardoj/fix/47162-play-video-console-error-when-backgrounded
Don't play video if app is not visible
2 parents 420c2a1 + b8a8358 commit 9819f37

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/components/VideoPlayerContexts/PlaybackContext.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {View} from 'react-native';
44
import type {VideoWithOnFullScreenUpdate} from '@components/VideoPlayer/types';
55
import useCurrentReportID from '@hooks/useCurrentReportID';
66
import usePrevious from '@hooks/usePrevious';
7+
import Visibility from '@libs/Visibility';
78
import type ChildrenProps from '@src/types/utils/ChildrenProps';
89
import type {PlaybackContext, StatusCallback} from './types';
910

@@ -19,6 +20,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
1920
const prevCurrentReportID = usePrevious(currentReportID);
2021
const videoResumeTryNumberRef = useRef<number>(0);
2122
const playVideoPromiseRef = useRef<Promise<AVPlaybackStatus>>();
23+
const isPlayPendingRef = useRef(false);
2224

2325
const pauseVideo = useCallback(() => {
2426
currentVideoPlayerRef.current?.setStatusAsync?.({shouldPlay: false});
@@ -29,6 +31,10 @@ function PlaybackContextProvider({children}: ChildrenProps) {
2931
}, [currentVideoPlayerRef]);
3032

3133
const playVideo = useCallback(() => {
34+
if (!Visibility.isVisible()) {
35+
isPlayPendingRef.current = true;
36+
return;
37+
}
3238
currentVideoPlayerRef.current?.getStatusAsync?.().then((status) => {
3339
const newStatus: AVPlaybackStatusToSet = {shouldPlay: true};
3440
if ('durationMillis' in status && status.durationMillis === status.positionMillis) {
@@ -94,12 +100,23 @@ function PlaybackContextProvider({children}: ChildrenProps) {
94100
// This prevents the video that plays when the app opens from being interrupted when currentReportID
95101
// is initially empty or '-1', or when it changes from empty/'-1' to another value
96102
// 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) {
98104
return;
99105
}
100106
resetVideoPlayerData();
101107
}, [currentReportID, prevCurrentReportID, resetVideoPlayerData]);
102108

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+
103120
const contextValue = useMemo(
104121
() => ({
105122
updateCurrentlyPlayingURL,

0 commit comments

Comments
 (0)