🎥 Make videos programmatically with React
fix audio / video rendering IDs
Jonny Burgerf45a1fd37413
diff --git a/packages/core/src/audio/AudioForRendering.tsx b/packages/core/src/audio/AudioForRendering.tsxindex 01fcdf9f3b..56882b8583 100644--- a/packages/core/src/audio/AudioForRendering.tsx+++ b/packages/core/src/audio/AudioForRendering.tsx@@ -1,5 +1,6 @@-import React, {useContext, useEffect, useState} from 'react';+import React, {useContext, useEffect, useMemo} from 'react';import {CompositionManager} from '../CompositionManager';+import {random} from '../random';import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';import {RemotionAudioProps} from './props';@@ -8,7 +9,12 @@ export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {const sequenceFrame = useCurrentFrame();const {registerAsset, unregisterAsset} = useContext(CompositionManager);- const [id] = useState(() => String(Math.random()));+ // Generate a string that's as unique as possible for this asset+ // but at the same time the same on all threads+ const id = useMemo(+ () => `audio-${random(props.src ?? '')}-muted:${props.muted}`,+ [props.muted, props.src]+ );useEffect(() => {if (!props.src) {diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsxindex 0c9faf57ad..9bfd2cd4f6 100644--- a/packages/core/src/video/VideoForRendering.tsx+++ b/packages/core/src/video/VideoForRendering.tsx@@ -1,7 +1,8 @@-import React, {useContext, useEffect, useRef, useState} from 'react';+import React, {useContext, useEffect, useMemo, useRef} from 'react';import {CompositionManager} from '../CompositionManager';import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';import {isApproximatelyTheSame} from '../is-approximately-the-same';+import {random} from '../random';import {continueRender, delayRender} from '../ready-manager';import {useCurrentFrame} from '../use-frame';import {useUnsafeVideoConfig} from '../use-unsafe-video-config';@@ -16,7 +17,12 @@ export const VideoForRendering: React.FC<RemotionVideoProps> = ({const videoRef = useRef<HTMLVideoElement>(null);const {registerAsset, unregisterAsset} = useContext(CompositionManager);- const [id] = useState(() => String(Math.random()));+ // Generate a string that's as unique as possible for this asset+ // but at the same time the same on all threads+ const id = useMemo(+ () => `audio-${random(props.src ?? '')}-muted:${props.muted}`,+ [props.muted, props.src]+ );if (!videoConfig) {throw new Error('No video config found');