Skip to content

Commit

Permalink
don't play video if app is not visible
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardoj committed Aug 12, 2024
1 parent 0980fcd commit b8a8358
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/components/VideoPlayerContexts/PlaybackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {View} from 'react-native';
import type {VideoWithOnFullScreenUpdate} from '@components/VideoPlayer/types';
import useCurrentReportID from '@hooks/useCurrentReportID';
import usePrevious from '@hooks/usePrevious';
import Visibility from '@libs/Visibility';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type {PlaybackContext, StatusCallback} from './types';

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

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

const playVideo = useCallback(() => {
if (!Visibility.isVisible()) {
isPlayPendingRef.current = true;
return;
}
currentVideoPlayerRef.current?.getStatusAsync?.().then((status) => {
const newStatus: AVPlaybackStatusToSet = {shouldPlay: true};
if ('durationMillis' in status && status.durationMillis === status.positionMillis) {
Expand Down Expand Up @@ -94,12 +100,23 @@ function PlaybackContextProvider({children}: ChildrenProps) {
// This prevents the video that plays when the app opens from being interrupted when currentReportID
// is initially empty or '-1', or when it changes from empty/'-1' to another value
// after the report screen in the central pane is mounted on the large screen.
if (!currentReportID || !prevCurrentReportID || currentReportID === '-1' || prevCurrentReportID === '-1') {
if (!currentReportID || !prevCurrentReportID || currentReportID === '-1' || prevCurrentReportID === '-1' || currentReportID === prevCurrentReportID) {
return;
}
resetVideoPlayerData();
}, [currentReportID, prevCurrentReportID, resetVideoPlayerData]);

useEffect(() => {
const unsubscribeVisibilityListener = Visibility.onVisibilityChange(() => {
if (!Visibility.isVisible() || !isPlayPendingRef.current) {
return;
}
playVideo();
isPlayPendingRef.current = false;
});
return unsubscribeVisibilityListener;
}, [playVideo]);

const contextValue = useMemo(
() => ({
updateCurrentlyPlayingURL,
Expand Down

0 comments on commit b8a8358

Please sign in to comment.