Skip to content

🎥 Make videos programmatically with React

`remotion`: Don't play audio on user event if it is premounting

Jonny Burgerf2629999adc2
diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsx
index 36dc21a012..132ab85adf 100644
--- a/packages/core/src/audio/AudioForPreview.tsx
+++ b/packages/core/src/audio/AudioForPreview.tsx
@@ -161,6 +161,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
showInTimeline,
premountDisplay: null,
onAutoPlayError: null,
+ isPremounting: Boolean(sequenceContext?.premounting),
});
useMediaPlayback({
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 437855fc43..79a0ea53a6 100644
--- a/packages/core/src/test/use-media-in-timeline.test.tsx
+++ b/packages/core/src/test/use-media-in-timeline.test.tsx
@@ -73,6 +73,7 @@ test('useMediaInTimeline registers and unregisters new sequence', () => {
showInTimeline: true,
premountDisplay: null,
onAutoPlayError: null,
+ isPremounting: false,
}),
{
wrapper,
diff --git a/packages/core/src/use-media-in-timeline.ts b/packages/core/src/use-media-in-timeline.ts
index e0bcf514dc..b2e2a30270 100644
--- a/packages/core/src/use-media-in-timeline.ts
+++ b/packages/core/src/use-media-in-timeline.ts
@@ -8,7 +8,7 @@ import {getRemotionEnvironment} from './get-remotion-environment.js';
import {useNonce} from './nonce.js';
import {playAndHandleNotAllowedError} from './play-and-handle-not-allowed-error.js';
import type {PlayableMediaTag} from './timeline-position-state.js';
-import {TimelineContext, usePlayingState} from './timeline-position-state.js';
+import {TimelineContext} from './timeline-position-state.js';
import {useVideoConfig} from './use-video-config.js';
import type {VolumeProp} from './volume-prop.js';
import {evaluateVolume} from './volume-prop.js';
@@ -37,6 +37,7 @@ export const useMediaInTimeline = ({
showInTimeline,
premountDisplay,
onAutoPlayError,
+ isPremounting,
}: {
volume: VolumeProp | undefined;
mediaVolume: number;
@@ -50,6 +51,7 @@ export const useMediaInTimeline = ({
showInTimeline: boolean;
premountDisplay: number | null;
onAutoPlayError: null | (() => void);
+ isPremounting: boolean;
}) => {
const videoConfig = useVideoConfig();
const {rootId, audioAndVideoTags} = useContext(TimelineContext);
@@ -57,7 +59,7 @@ export const useMediaInTimeline = ({
const actualFrom = parentSequence
? parentSequence.relativeFrom + parentSequence.cumulatedFrom
: 0;
- const [playing] = usePlayingState();
+ const {imperativePlaying} = useContext(TimelineContext);
const startsAt = useMediaStartsAt();
const {registerSequence, unregisterSequence} = useContext(SequenceManager);
const [initialVolume] = useState<VolumeProp | undefined>(() => volume);
@@ -164,11 +166,15 @@ export const useMediaInTimeline = ({
const tag: PlayableMediaTag = {
id,
play: () => {
- if (!playing) {
+ if (!imperativePlaying.current) {
// Don't play if for example in a <Freeze> state.
return;
}
+ if (isPremounting) {
+ return;
+ }
+
return playAndHandleNotAllowedError(
mediaRef,
mediaType,
@@ -183,5 +189,13 @@ export const useMediaInTimeline = ({
(a) => a.id !== id,
);
};
- }, [audioAndVideoTags, id, mediaRef, mediaType, onAutoPlayError, playing]);
+ }, [
+ audioAndVideoTags,
+ id,
+ mediaRef,
+ mediaType,
+ onAutoPlayError,
+ imperativePlaying,
+ isPremounting,
+ ]);
};
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index a096d9bf0a..9531140685 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -105,6 +105,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
showInTimeline,
premountDisplay: null,
onAutoPlayError: onAutoPlayError ?? null,
+ isPremounting: Boolean(parentSequence?.premounting),
});
useSyncVolumeWithMediaTag({