Skip to content

🎥 Make videos programmatically with React

`remotion`: Battle-proof shared audio tags for different React lifecycle order

Jonny Burger72e148071a7c
diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsx
index f66a91b8c9..8408d00fb0 100644
--- a/packages/core/src/audio/AudioForPreview.tsx
+++ b/packages/core/src/audio/AudioForPreview.tsx
@@ -173,6 +173,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
aud: propsToPass,
audioId: id,
premounting: Boolean(sequenceContext?.premounting),
+ postmounting: Boolean(sequenceContext?.postmounting),
});
useMediaInTimeline({
diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsx
index 1657279e94..14a6cc476d 100644
--- a/packages/core/src/audio/shared-audio-tags.tsx
+++ b/packages/core/src/audio/shared-audio-tags.tsx
@@ -34,6 +34,8 @@ type AudioElem = {
audioId: string;
mediaElementSourceNode: SharedElementSourceNode | null;
premounting: boolean;
+ postmounting: boolean;
+ audioMounted: boolean;
};
const EMPTY_AUDIO =
@@ -44,6 +46,7 @@ type SharedContext = {
aud: AudioHTMLAttributes<HTMLAudioElement>;
audioId: string;
premounting: boolean;
+ postmounting: boolean;
}) => AudioElem;
unregisterAudio: (id: number) => void;
updateAudio: (options: {
@@ -51,6 +54,7 @@ type SharedContext = {
aud: AudioHTMLAttributes<HTMLAudioElement>;
audioId: string;
premounting: boolean;
+ postmounting: boolean;
}) => void;
playAllAudios: () => void;
numberOfAudioTags: number;
@@ -180,8 +184,9 @@ export const SharedAudioContextProvider: React.FC<{
aud: AudioHTMLAttributes<HTMLAudioElement>;
audioId: string;
premounting: boolean;
+ postmounting: boolean;
}) => {
- const {aud, audioId, premounting} = options;
+ const {aud, audioId, premounting, postmounting} = options;
const found = audios.current?.find((a) => a.audioId === audioId);
if (found) {
return found;
@@ -208,6 +213,8 @@ export const SharedAudioContextProvider: React.FC<{
audioId,
mediaElementSourceNode,
premounting,
+ audioMounted: Boolean(ref.current),
+ postmounting,
};
audios.current?.push(newElem);
rerenderAudios();
@@ -249,6 +256,11 @@ export const SharedAudioContextProvider: React.FC<{
let changed = false;
audios.current = audios.current?.map((prevA): AudioElem => {
+ const audioMounted = Boolean(prevA.el.current);
+ if (prevA.audioMounted !== audioMounted) {
+ changed = true;
+ }
+
if (prevA.id === id) {
const isTheSame =
compareProps(
@@ -260,11 +272,13 @@ export const SharedAudioContextProvider: React.FC<{
}
changed = true;
+
return {
...prevA,
props: aud,
premounting,
audioId,
+ audioMounted,
};
}
@@ -339,10 +353,12 @@ export const useSharedAudio = ({
aud,
audioId,
premounting,
+ postmounting,
}: {
aud: AudioHTMLAttributes<HTMLAudioElement>;
audioId: string;
premounting: boolean;
+ postmounting: boolean;
}) => {
const ctx = useContext(SharedAudioContext);
@@ -351,7 +367,7 @@ export const useSharedAudio = ({
*/
const [elem] = useState((): AudioElem => {
if (ctx && ctx.numberOfAudioTags > 0) {
- return ctx.registerAudio({aud, audioId, premounting});
+ return ctx.registerAudio({aud, audioId, premounting, postmounting});
}
const el = React.createRef<HTMLAudioElement>();
@@ -369,6 +385,8 @@ export const useSharedAudio = ({
audioId,
mediaElementSourceNode,
premounting,
+ audioMounted: Boolean(el.current),
+ postmounting,
};
});
@@ -384,7 +402,7 @@ export const useSharedAudio = ({
if (typeof document !== 'undefined') {
effectToUse(() => {
if (ctx && ctx.numberOfAudioTags > 0) {
- ctx.updateAudio({id: elem.id, aud, audioId, premounting});
+ ctx.updateAudio({id: elem.id, aud, audioId, premounting, postmounting});
}
}, [aud, ctx, elem.id, audioId, premounting]);