Skip to content

🎥 Make videos programmatically with React

fix safari case

Jonny Burger9cbae77878d9
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index 1353984996..f656bc4a01 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -167,13 +167,27 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
};
}, [src]);
+ useEffect(() => {
+ const {current} = videoRef;
+
+ if (!current) {
+ return;
+ }
+
+ // Without this, on iOS Safari, the video cannot be seeked.
+ // if a seek is triggered before `loadedmetadata` is fired,
+ // the video will not seek, even if `loadedmetadata` is fired afterwards.
+
+ if (isIosSafari()) {
+ current.preload = 'metadata';
+ } else {
+ current.preload = 'auto';
+ }
+ }, []);
+
return (
<video
ref={videoRef}
- // Without this, on iOS Safari, the video cannot be seeked.
- // if a seek is triggered before `loadedmetadata` is fired,
- // the video will not seek, even if `loadedmetadata` is fired afterwards.
- preload={isIosSafari() ? 'metadata' : 'auto'}
muted={muted || mediaMuted}
playsInline
src={actualSrc}
diff --git a/packages/core/src/video/video-fragment.ts b/packages/core/src/video/video-fragment.ts
index 16fe5610a6..394a9c3393 100644
--- a/packages/core/src/video/video-fragment.ts
+++ b/packages/core/src/video/video-fragment.ts
@@ -4,21 +4,16 @@ const toSeconds = (time: number, fps: number) => {
return Math.round((time / fps) * 100) / 100;
};
-// https://github.com/remotion-dev/remotion/issues/1655
-const isIOSSafariCase = (actualSrc: string) => {
- if (typeof window === 'undefined') {
- return false;
- }
-
- const isIpadIPodIPhone = /iP(ad|od|hone)/i.test(window.navigator.userAgent);
- const isSafari = Boolean(
- navigator.userAgent.match(/Version\/[\d.]+.*Safari/),
- );
- const isChrome = Boolean(navigator.userAgent.match(/CriOS\//));
+export const isIosSafari = () => {
+ return typeof window === 'undefined'
+ ? false
+ : /iP(ad|od|hone)/i.test(window.navigator.userAgent) &&
+ Boolean(navigator.userAgent.match(/Version\/[\d.]+.*Safari/));
+};
- return (
- isIpadIPodIPhone && (isSafari || isChrome) && actualSrc.startsWith('blob:')
- );
+// https://github.com/remotion-dev/remotion/issues/1655
+const isIOSSafariAndBlob = (actualSrc: string) => {
+ return isIosSafari() && actualSrc.startsWith('blob:');
};
export const appendVideoFragment = ({
@@ -32,7 +27,7 @@ export const appendVideoFragment = ({
duration: number;
fps: number;
}): string => {
- if (isIOSSafariCase(actualSrc)) {
+ if (isIOSSafariAndBlob(actualSrc)) {
return actualSrc;
}
@@ -109,10 +104,3 @@ export const useAppendVideoFragment = ({
return appended;
};
-
-export const isIosSafari = () => {
- return typeof window === 'undefined'
- ? false
- : /iP(ad|od|hone)/i.test(window.navigator.userAgent) &&
- Boolean(navigator.userAgent.match(/Version\/[\d.]+.*Safari/));
-};