Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Safari - Unable to play video attachment #54519

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions assets/images/video-slash.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 14 additions & 2 deletions src/components/AttachmentOfflineIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 12,11 @@ import Text from './Text';
type AttachmentOfflineIndicatorProps = {
/** Whether the offline indicator is displayed for the attachment preview. */
isPreview?: boolean;
/** Whether the offline indicator should always have a background color set. */
shouldAlwaysHaveBackground?: boolean;
};

function AttachmentOfflineIndicator({isPreview = false}: AttachmentOfflineIndicatorProps) {
function AttachmentOfflineIndicator({isPreview = false, shouldAlwaysHaveBackground = false}: AttachmentOfflineIndicatorProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {isOffline} = useNetwork();
Expand All @@ -35,7 37,17 @@ function AttachmentOfflineIndicator({isPreview = false}: AttachmentOfflineIndica
}

return (
<View style={[styles.flexColumn, styles.alignItemsCenter, styles.justifyContentCenter, styles.pAbsolute, styles.h100, styles.w100, isPreview && styles.hoveredComponentBG]}>
<View
style={[
styles.flexColumn,
styles.alignItemsCenter,
styles.justifyContentCenter,
styles.pAbsolute,
styles.h100,
styles.w100,
(isPreview || shouldAlwaysHaveBackground) && styles.hoveredComponentBG,
]}
>
<Icon
fill={theme.icon}
src={Expensicons.OfflineCloud}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Icon/Expensicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 197,7 @@ import UserEye from '@assets/images/user-eye.svg';
import UserPlus from '@assets/images/user-plus.svg';
import User from '@assets/images/user.svg';
import Users from '@assets/images/users.svg';
import VideoSlash from '@assets/images/video-slash.svg';
import VolumeHigh from '@assets/images/volume-high.svg';
import VolumeLow from '@assets/images/volume-low.svg';
import Wallet from '@assets/images/wallet.svg';
Expand Down Expand Up @@ -371,6 372,7 @@ export {
User,
UserCheck,
Users,
VideoSlash,
VolumeHigh,
VolumeLow,
Wallet,
Expand Down
69 changes: 41 additions & 28 deletions src/components/VideoPlayer/BaseVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,7 @@ import CONST from '@src/CONST';
import shouldReplayVideo from './shouldReplayVideo';
import type {VideoPlayerProps, VideoWithOnFullScreenUpdate} from './types';
import * as VideoUtils from './utils';
import VideoErrorIndicator from './VideoErrorIndicator';
import VideoPlayerControls from './VideoPlayerControls';

function BaseVideoPlayer({
Expand Down Expand Up @@ -72,6 73,7 @@ function BaseVideoPlayer({
const [isLoading, setIsLoading] = useState(true);
const [isEnded, setIsEnded] = useState(false);
const [isBuffering, setIsBuffering] = useState(true);
const [isError, setIsError] = useState(false);
// we add "#t=0.001" at the end of the URL to skip first milisecond of the video and always be able to show proper video preview when video is paused at the beginning
const [sourceURL] = useState(() => VideoUtils.addSkipTimeTagToURL(url.includes('blob:') || url.includes('file:///') ? url : addEncryptedAuthTokenToURL(url), 0.001));
const [isPopoverVisible, setIsPopoverVisible] = useState(false);
Expand Down Expand Up @@ -438,37 440,48 @@ function BaseVideoPlayer({
videoPlayerElementParentRef.current = el;
}}
>
<Video
ref={videoPlayerRef}
style={[styles.w100, styles.h100, videoPlayerStyle]}
videoStyle={[styles.w100, styles.h100, videoStyle]}
source={{
// if video is loading and is offline, we want to change uri to "" to
// reset the video player after connection is back
uri: !isLoading || (isLoading && !isOffline) ? sourceURL : '',
}}
shouldPlay={shouldPlay}
useNativeControls={false}
resizeMode={resizeMode as ResizeMode}
isLooping={isLooping}
onReadyForDisplay={(e) => {
if (isCurrentlyURLSet && !isUploading) {
playVideo();
}
onVideoLoaded?.(e);
if (shouldUseNewRate) {
return;
}
videoPlayerRef.current?.setStatusAsync?.({rate: currentPlaybackSpeed});
}}
onPlaybackStatusUpdate={handlePlaybackStatusUpdate}
onFullscreenUpdate={handleFullscreenUpdate}
/>
<View style={styles.flex1}>
<Video
ref={videoPlayerRef}
style={[styles.w100, styles.h100, videoPlayerStyle]}
videoStyle={[styles.w100, styles.h100, videoStyle]}
source={{
// if video is loading and is offline, we want to change uri to "" to
// reset the video player after connection is back
uri: !isLoading || (isLoading && !isOffline) ? sourceURL : '',
}}
shouldPlay={shouldPlay}
useNativeControls={false}
resizeMode={resizeMode as ResizeMode}
isLooping={isLooping}
onReadyForDisplay={(e) => {
if (isCurrentlyURLSet && !isUploading) {
playVideo();
}
onVideoLoaded?.(e);
if (shouldUseNewRate) {
return;
}
videoPlayerRef.current?.setStatusAsync?.({rate: currentPlaybackSpeed});
}}
onPlaybackStatusUpdate={handlePlaybackStatusUpdate}
onFullscreenUpdate={handleFullscreenUpdate}
onError={() => setIsError(true)}
/>
{!isError && ((isLoading && !isOffline) || (isBuffering && !isPlaying)) && (
<FullScreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />
)}
{isError && <VideoErrorIndicator />}
</View>
</View>
)}
</PressableWithoutFeedback>
{((isLoading && !isOffline) || (isBuffering && !isPlaying)) && <FullScreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && (isOffline || !isBuffering) && <AttachmentOfflineIndicator isPreview={isPreview} />}
{isLoading && (isOffline || !isBuffering) && (
<AttachmentOfflineIndicator
isPreview={isPreview}
shouldAlwaysHaveBackground
/>
)}
{controlStatusState !== CONST.VIDEO_PLAYER.CONTROLS_STATUS.HIDE && !isLoading && (isPopoverVisible || isHovered || canUseTouchScreen || isEnded) && (
<VideoPlayerControls
duration={duration}
Expand Down
27 changes: 27 additions & 0 deletions src/components/VideoPlayer/VideoErrorIndicator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 1,27 @@
import React from 'react';
import {View} from 'react-native';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import variables from '@styles/variables';

function VideoErrorIndicator() {
const theme = useTheme();
const styles = useThemeStyles();

return (
<View style={[styles.flexColumn, styles.alignItemsCenter, styles.justifyContentCenter, styles.pAbsolute, styles.h100, styles.w100, styles.highlightBG]}>
<Icon
fill={theme.activeComponentBG}
src={Expensicons.VideoSlash}
width={variables.iconSizeSuperLarge}
height={variables.iconSizeSuperLarge}
/>
</View>
);
}

VideoErrorIndicator.displayName = 'VideoErrorIndicator';

export default VideoErrorIndicator;
2 changes: 1 addition & 1 deletion src/components/VideoPlayerContexts/PlaybackContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 40,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
if ('durationMillis' in status && status.durationMillis === status.positionMillis) {
newStatus.positionMillis = 0;
}
playVideoPromiseRef.current = currentVideoPlayerRef.current?.setStatusAsync(newStatus);
playVideoPromiseRef.current = currentVideoPlayerRef.current?.setStatusAsync(newStatus).catch(() => {});
});
}, [currentVideoPlayerRef]);

Expand Down
Loading