Skip to content

🎥 Make videos programmatically with React

`remotion`: New `onAutoPlayError` error callback

Jonny Burger9f849c35e8f6
diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsx
index 07bb92514b..1f30dc8e75 100644
--- a/packages/core/src/audio/AudioForPreview.tsx
+++ b/packages/core/src/audio/AudioForPreview.tsx
@@ -160,6 +160,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
stack: _remotionInternalStack,
showInTimeline,
premountDisplay: null,
+ onAutoPlayError: null,
});
useMediaPlayback({
@@ -173,6 +174,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
isPremounting: Boolean(sequenceContext?.premounting),
pauseWhenBuffering,
debugSeeking: _remotionDebugSeeking,
+ onAutoPlayError: null,
});
useImperativeHandle(
diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsx
index 5199d36484..6507d0147c 100644
--- a/packages/core/src/audio/shared-audio-tags.tsx
+++ b/packages/core/src/audio/shared-audio-tags.tsx
@@ -94,9 +94,11 @@ const didPropChange = (key: string, newProp: unknown, prevProp: unknown) => {
export const SharedAudioContext = createContext<SharedContext | null>(null);
export const SharedAudioContextProvider: React.FC<{
- numberOfAudioTags: number;
- children: React.ReactNode;
- component: LazyExoticComponent<ComponentType<Record<string, unknown>>> | null;
+ readonly numberOfAudioTags: number;
+ readonly children: React.ReactNode;
+ readonly component: LazyExoticComponent<
+ ComponentType<Record<string, unknown>>
+ > | null;
}> = ({children, numberOfAudioTags, component}) => {
const audios = useRef<AudioElem[]>([]);
const [initialNumberOfAudioTags] = useState(numberOfAudioTags);
@@ -237,7 +239,7 @@ export const SharedAudioContextProvider: React.FC<{
const playAllAudios = useCallback(() => {
refs.forEach((ref) => {
- playAndHandleNotAllowedError(ref.ref, 'audio');
+ playAndHandleNotAllowedError(ref.ref, 'audio', null);
});
}, [refs]);
diff --git a/packages/core/src/play-and-handle-not-allowed-error.ts b/packages/core/src/play-and-handle-not-allowed-error.ts
index 9b1bd9bccd..18885e7622 100644
--- a/packages/core/src/play-and-handle-not-allowed-error.ts
+++ b/packages/core/src/play-and-handle-not-allowed-error.ts
@@ -1,8 +1,10 @@
import type {RefObject} from 'react';
+import {getRemotionEnvironment} from './get-remotion-environment';
export const playAndHandleNotAllowedError = (
mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>,
mediaType: 'audio' | 'video',
+ onAutoPlayError: null | (() => void),
) => {
const {current} = mediaRef;
if (!current) {
@@ -53,8 +55,18 @@ export const playAndHandleNotAllowedError = (
// eslint-disable-next-line no-console
console.log(`Could not play ${mediaType} due to following error: `, err);
if (!current.muted) {
+ if (onAutoPlayError) {
+ onAutoPlayError();
+ return;
+ }
+
// eslint-disable-next-line no-console
console.log(`The video will be muted and we'll retry playing it.`);
+ if (mediaType === 'video' && getRemotionEnvironment().isPlayer) {
+ // eslint-disable-next-line no-console
+ console.log('Use onAutoPlayError() to handle this error yourself.');
+ }
+
current.muted = true;
current.play();
}
diff --git a/packages/core/src/test/use-media-in-timeline.test.tsx b/packages/core/src/test/use-media-in-timeline.test.tsx
index 947b53e845..b92db3c215 100644
--- a/packages/core/src/test/use-media-in-timeline.test.tsx
+++ b/packages/core/src/test/use-media-in-timeline.test.tsx
@@ -72,6 +72,7 @@ test('useMediaInTimeline registers and unregisters new sequence', () => {
stack: null,
showInTimeline: true,
premountDisplay: null,
+ onAutoPlayError: null,
}),
{
wrapper,
diff --git a/packages/core/src/use-media-in-timeline.ts b/packages/core/src/use-media-in-timeline.ts
index 0ff916ce01..c9397b9af1 100644
--- a/packages/core/src/use-media-in-timeline.ts
+++ b/packages/core/src/use-media-in-timeline.ts
@@ -36,6 +36,7 @@ export const useMediaInTimeline = ({
stack,
showInTimeline,
premountDisplay,
+ onAutoPlayError,
}: {
volume: VolumeProp | undefined;
mediaVolume: number;
@@ -48,6 +49,7 @@ export const useMediaInTimeline = ({
stack: string | null;
showInTimeline: boolean;
premountDisplay: number | null;
+ onAutoPlayError: null | (() => void);
}) => {
const videoConfig = useVideoConfig();
const {rootId, audioAndVideoTags} = useContext(TimelineContext);
@@ -167,7 +169,11 @@ export const useMediaInTimeline = ({
return;
}
- return playAndHandleNotAllowedError(mediaRef, mediaType);
+ return playAndHandleNotAllowedError(
+ mediaRef,
+ mediaType,
+ onAutoPlayError,
+ );
},
};
audioAndVideoTags.current.push(tag);
@@ -177,5 +183,5 @@ export const useMediaInTimeline = ({
(a) => a.id !== id,
);
};
- }, [audioAndVideoTags, id, mediaRef, mediaType, playing]);
+ }, [audioAndVideoTags, id, mediaRef, mediaType, onAutoPlayError, playing]);
};
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
index d773d94a43..c01c72682c 100644
--- a/packages/core/src/use-media-playback.ts
+++ b/packages/core/src/use-media-playback.ts
@@ -42,6 +42,7 @@ export const useMediaPlayback = ({
pauseWhenBuffering,
isPremounting,
debugSeeking,
+ onAutoPlayError,
}: {
mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>;
src: string | undefined;
@@ -52,6 +53,7 @@ export const useMediaPlayback = ({
pauseWhenBuffering: boolean;
isPremounting: boolean;
debugSeeking: boolean;
+ onAutoPlayError: null | (() => void);
}) => {
const {playbackRate: globalPlaybackRate} = useContext(TimelineContext);
const frame = useCurrentFrame();
@@ -209,7 +211,7 @@ export const useMediaPlayback = ({
if (playing && !isVariableFpsVideo) {
bufferUntilFirstFrame(shouldBeTime);
if (mediaRef.current.paused) {
- playAndHandleNotAllowedError(mediaRef, mediaType);
+ playAndHandleNotAllowedError(mediaRef, mediaType, onAutoPlayError);
}
}
@@ -254,7 +256,7 @@ export const useMediaPlayback = ({
seek(mediaRef, shouldBeTime);
}
- playAndHandleNotAllowedError(mediaRef, mediaType);
+ playAndHandleNotAllowedError(mediaRef, mediaType, onAutoPlayError);
if (!isVariableFpsVideo) {
bufferUntilFirstFrame(shouldBeTime);
}
@@ -275,5 +277,6 @@ export const useMediaPlayback = ({
playbackRate,
playing,
src,
+ onAutoPlayError,
]);
};
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index 3e815e4f09..8c67b60be0 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -72,6 +72,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
transparent,
toneMapped,
_remotionDebugSeeking,
+ onAutoPlayError,
...withoutTransparent
} = otherProps;
@@ -84,6 +85,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
onlyWarnForMediaSeekingError
pauseWhenBuffering={pauseWhenBuffering ?? false}
showInTimeline={showInTimeline ?? true}
+ onAutoPlayError={onAutoPlayError ?? undefined}
{...withoutTransparent}
/>
);
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 164165d9d1..13140b29fa 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -33,6 +33,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
stack,
_remotionInternalNativeLoopPassed,
showInTimeline,
+ onAutoPlayError,
...otherProps
} = props;
const {loop, _remotionDebugSeeking, ...propsOtherThanLoop} = props;
@@ -133,6 +134,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
}
_remotionDebugSeeking={_remotionDebugSeeking ?? false}
showInTimeline={showInTimeline ?? true}
+ onAutoPlayError={onAutoPlayError ?? undefined}
/>
);
};
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index a74db187b3..dcb15d7206 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -62,6 +62,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
showInTimeline,
loopVolumeCurveBehavior,
onError,
+ onAutoPlayError,
...nativeProps
} = props;
@@ -98,6 +99,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
stack: _remotionInternalStack,
showInTimeline,
premountDisplay: null,
+ onAutoPlayError: onAutoPlayError ?? null,
});
useSyncVolumeWithMediaTag({
@@ -119,6 +121,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
isPremounting: Boolean(parentSequence?.premounting),
pauseWhenBuffering,
debugSeeking: _remotionDebugSeeking,
+ onAutoPlayError: onAutoPlayError ?? null,
});
const actualFrom = parentSequence ? parentSequence.relativeFrom : 0;
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index e461366e54..6c73060e45 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -36,6 +36,7 @@ export type RemotionVideoProps = Omit<
loopVolumeCurveBehavior?: LoopVolumeCurveBehavior;
delayRenderRetries?: number;
onError?: (err: Error) => void;
+ onAutoPlayError?: () => void;
};
type DeprecatedOffthreadVideoProps = {
@@ -69,5 +70,6 @@ export type OffthreadVideoProps = {
*/
stack?: string;
showInTimeline?: boolean;
+ onAutoPlayError?: null | (() => void);
} & RemotionMainVideoProps &
DeprecatedOffthreadVideoProps;
diff --git a/packages/docs/docs/offthreadvideo.mdx b/packages/docs/docs/offthreadvideo.mdx
index 586691b577..fad17dd407 100644
--- a/packages/docs/docs/offthreadvideo.mdx
+++ b/packages/docs/docs/offthreadvideo.mdx
@@ -254,6 +254,13 @@ Customize the [timeout](/docs/delay-render#modifying-the-timeout) of the [`delay
Customize the [number of retries](/docs/delay-render#retrying) of the [`delayRender()`](/docs/delay-render) call that this component makes.
+### `onAutoPlayError`<AvailableFrom v="4.0.187" />
+
+A callback function that gets called when the video fails to play due to autoplay restrictions.
+If you don't pass a callback, the video will be muted and be retried once.
+This prop is useful if you want to handle the error yourself, e.g. for pausing the Player.
+Read more here about [autoplay restrictions](/docs/player/autoplay).
+
### ~~`imageFormat` <AvailableFrom v="3.0.22" />~~
_removed in v4.0.0_
diff --git a/packages/docs/docs/player/autoplay.mdx b/packages/docs/docs/player/autoplay.mdx
index 112f92b8ef..89926af077 100644
--- a/packages/docs/docs/player/autoplay.mdx
+++ b/packages/docs/docs/player/autoplay.mdx
@@ -57,3 +57,8 @@ You can have as many silent audio tags as you want. Set `numberOfSharedAudioTags
:::warning
Due to a bug on our side, this prop does not work well [before Remotion v3.1.5](https://github.com/remotion-dev/remotion/issues/723). Upgrade to receive the bugfix.
:::
+
+## Controlling the behavior of autoplay failures<AvailableFrom v="4.0.187" />
+
+If the browser blocks a video from being autoplayed due to it having sound, the video will be muted and will be retried once.
+If you want to handle the error yourself, you can pass a callback to the [`onAutoPlayError`](/docs/offthreadvideo#onautoplayerror) prop of `<OffthreadVideo>` and `<Video>`.
diff --git a/packages/docs/docs/video.mdx b/packages/docs/docs/video.mdx
index 541bc29d9d..f08bacd963 100644
--- a/packages/docs/docs/video.mdx
+++ b/packages/docs/docs/video.mdx
@@ -241,6 +241,13 @@ Customize the [timeout](/docs/delay-render#modifying-the-timeout) of the [`delay
Customize the [number of retries](/docs/delay-render#retrying) of the [`delayRender()`](/docs/delay-render) call that this component makes.
+### `onAutoPlayError`<AvailableFrom v="4.0.187" />
+
+A callback function that gets called when the video fails to play due to autoplay restrictions.
+If you don't pass a callback, the video will be muted and be retried once.
+This prop is useful if you want to handle the error yourself, e.g. for pausing the Player.
+Read more here about [autoplay restrictions](/docs/player/autoplay).
+
## Speed up renders for video with silent audio
Remotion will download the whole video during render in order to mix its audio. If the video contains a silent audio track, you can add the muted property to signal to Remotion that it does not need to download the video and make the render more efficient.