Skip to content

Commit 9f8fb9f

Browse files
authored
Merge pull request Expensify#85195 from yuvrajangadsingh/fix/84443-thread-playback-speed
fix: sync playback speed between parent and thread video players
2 parents 7713882 + 505dece commit 9f8fb9f

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/components/VideoPlayer/BaseVideoPlayer.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {useSession} from '@components/OnyxListItemProvider';
1515
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
1616
import {useFullScreenState} from '@components/VideoPlayerContexts/FullScreenContextProvider';
1717
import {usePlaybackActionsContext, usePlaybackStateContext} from '@components/VideoPlayerContexts/PlaybackContext';
18-
import type {PlaybackSpeed} from '@components/VideoPlayerContexts/types';
1918
import {useVideoPopoverMenuActions} from '@components/VideoPlayerContexts/VideoPopoverMenuContext';
2019
import {useVolumeActions, useVolumeState} from '@components/VideoPlayerContexts/VolumeContext';
2120
import VideoPopoverMenu from '@components/VideoPopoverMenu';
@@ -165,7 +164,7 @@ function BaseVideoPlayer({
165164
isLocalFile: isUploading,
166165
});
167166

168-
const {updateVideoPopoverMenuPlayerRef, updatePlaybackSpeed, updateSource: updatePopoverMenuSource} = useVideoPopoverMenuActions();
167+
const {updateVideoPopoverMenuPlayerRef, updateSource: updatePopoverMenuSource} = useVideoPopoverMenuActions();
169168

170169
const togglePlayCurrentVideo = useCallback(() => {
171170
if (!isCurrentlyURLSet) {
@@ -246,10 +245,9 @@ function BaseVideoPlayer({
246245

247246
const showPopoverMenu = (event?: GestureResponderEvent | KeyboardEvent) => {
248247
updateVideoPopoverMenuPlayerRef(videoPlayerRef.current);
249-
if (!videoPlayerRef.current?.playbackRate) {
248+
if (!videoPlayerRef.current) {
250249
return;
251250
}
252-
updatePlaybackSpeed(videoPlayerRef.current.playbackRate as PlaybackSpeed);
253251
setIsPopoverVisible(true);
254252

255253
updatePopoverMenuSource(url);

src/components/VideoPlayerContexts/VideoPopoverMenuContext.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {VideoPlayer} from 'expo-video';
2-
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
2+
import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
33
import {useSession} from '@components/OnyxListItemProvider';
44
import type {PopoverMenuItem} from '@components/PopoverMenu';
55
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
@@ -9,6 +9,7 @@ import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
99
import fileDownload from '@libs/fileDownload';
1010
import CONST from '@src/CONST';
1111
import type ChildrenProps from '@src/types/utils/ChildrenProps';
12+
import {usePlaybackStateContext} from './PlaybackContext';
1213
import type {PlaybackSpeed, VideoPopoverMenuActionsContextType, VideoPopoverMenuStateContextType} from './types';
1314

1415
const VideoPopoverMenuStateContext = React.createContext<VideoPopoverMenuStateContextType | null>(null);
@@ -17,6 +18,7 @@ const VideoPopoverMenuActionsContext = React.createContext<VideoPopoverMenuActio
1718
function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
1819
const icons = useMemoizedLazyExpensifyIcons(['Checkmark', 'Download', 'Meter'] as const);
1920
const {translate} = useLocalize();
21+
const {currentVideoPlayerRef, originalParent} = usePlaybackStateContext();
2022
const [source, setSource] = useState('');
2123
const [currentPlaybackSpeed, setCurrentPlaybackSpeed] = useState<PlaybackSpeed>(CONST.VIDEO_PLAYER.PLAYBACK_SPEEDS[3]);
2224
const {isOffline} = useNetwork();
@@ -39,6 +41,18 @@ function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
3941
[videoPopoverMenuPlayerRef],
4042
);
4143

44+
// Apply stored playback speed when the active player changes (e.g. navigating from parent to thread).
45+
// Same pattern as VolumeContext which re-applies volume on originalParent change.
46+
useEffect(() => {
47+
if (!originalParent || !currentVideoPlayerRef.current) {
48+
return;
49+
}
50+
51+
if (currentVideoPlayerRef.current.playbackRate !== currentPlaybackSpeed) {
52+
currentVideoPlayerRef.current.playbackRate = currentPlaybackSpeed;
53+
}
54+
}, [originalParent, currentPlaybackSpeed]);
55+
4256
const updateVideoPopoverMenuPlayerRef = (videoPlayer: VideoPlayer | null) => {
4357
videoPopoverMenuPlayerRef.current = videoPlayer;
4458
};

0 commit comments

Comments
 (0)