Skip to content

🎥 Make videos programmatically with React

report transition target

Jonny Burger2e2eb0479664
diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsx
index fa68202c4a..257d99302f 100644
--- a/packages/core/src/audio/shared-audio-tags.tsx
+++ b/packages/core/src/audio/shared-audio-tags.tsx
@@ -14,6 +14,7 @@ import {playAndHandleNotAllowedError} from '../play-and-handle-not-allowed-error
import {useRemotionEnvironment} from '../use-remotion-environment.js';
import type {SharedElementSourceNode} from './shared-element-source-node.js';
import {makeSharedElementSourceNode} from './shared-element-source-node.js';
+import type {RemotionAudioContextState} from './use-audio-context.js';
import {useSingletonAudioContext} from './use-audio-context.js';
import {waitUntilActuallyResumed} from './wait-until-actually-resumed.js';
@@ -75,7 +76,7 @@ export type AudioSyncAnchorEmitter = {
type SharedAudioContextValue = {
audioContext: AudioContext | null;
- getAudioContextState: () => AudioContextState | null;
+ getAudioContextState: () => RemotionAudioContextState | null;
gainNode: GainNode | null;
audioSyncAnchor: {value: number};
audioSyncAnchorEmitter: AudioSyncAnchorEmitter;
diff --git a/packages/core/src/audio/use-audio-context.ts b/packages/core/src/audio/use-audio-context.ts
index 8f02360636..29ef45145f 100644
--- a/packages/core/src/audio/use-audio-context.ts
+++ b/packages/core/src/audio/use-audio-context.ts
@@ -3,6 +3,14 @@ import type {LogLevel} from '../log';
import {Log} from '../log';
import {useRemotionEnvironment} from '../use-remotion-environment';
+// The native AudioContext.state can be 'closed' | 'interrupted' | 'running' | 'suspended'.
+// resume() and suspend() do not change the state immediately, so we expose two
+// additional transition states to reflect that a change is in progress.
+export type RemotionAudioContextState =
+ | AudioContextState
+ | 'running-to-suspended'
+ | 'suspended-to-running';
+
let warned = false;
const warnOnce = (logLevel: LogLevel) => {
@@ -60,9 +68,49 @@ export const useSingletonAudioContext = ({
audioContext.suspend();
- const getState = () => audioContext.state;
- const resume = () => audioContext.resume();
- const suspend = () => audioContext.suspend();
+ // Tracks the state we are transitioning towards while resume()/suspend()
+ // have been called but the native state has not updated yet.
+ let transitionTarget: 'running' | 'suspended' | null = null;
+
+ const getState = (): RemotionAudioContextState => {
+ const nativeState = audioContext.state;
+
+ if (transitionTarget === 'running' && nativeState !== 'running') {
+ return 'suspended-to-running';
+ }
+
+ if (transitionTarget === 'suspended' && nativeState !== 'suspended') {
+ return 'running-to-suspended';
+ }
+
+ return nativeState;
+ };
+
+ const resume = () => {
+ transitionTarget = 'running';
+ const promise = audioContext.resume();
+
+ promise.finally(() => {
+ if (transitionTarget === 'running') {
+ transitionTarget = null;
+ }
+ });
+
+ return promise;
+ };
+
+ const suspend = () => {
+ transitionTarget = 'suspended';
+ const promise = audioContext.suspend();
+
+ promise.finally(() => {
+ if (transitionTarget === 'suspended') {
+ transitionTarget = null;
+ }
+ });
+
+ return promise;
+ };
return {
audioContext,