Skip to content

🎥 Make videos programmatically with React

Fix audio playback stalls from overlapping resume and suspend

Jonny Burger6312c557d90d
diff --git a/packages/core/src/audio/use-audio-context.ts b/packages/core/src/audio/use-audio-context.ts
index 179fb8b2bd..8efd53da64 100644
--- a/packages/core/src/audio/use-audio-context.ts
+++ b/packages/core/src/audio/use-audio-context.ts
@@ -80,6 +80,8 @@ export const useSingletonAudioContext = ({
// 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;
+ let transitionId = 0;
+ let pendingResume: Promise<void> | null = null;
const getState = (): RemotionAudioContextState => {
const nativeState = audioContext.state;
@@ -96,29 +98,46 @@ export const useSingletonAudioContext = ({
};
const resume = () => {
+ const id = ++transitionId;
transitionTarget = 'running';
+ // Keep resume synchronous so it retains the browser's user activation.
const promise = audioContext.resume();
+ pendingResume = promise;
- promise.finally(() => {
- if (transitionTarget === 'running') {
+ return promise.finally(() => {
+ if (pendingResume === promise) {
+ pendingResume = null;
+ }
+
+ if (transitionId === id) {
transitionTarget = null;
}
});
-
- return promise;
};
const suspend = () => {
+ const id = ++transitionId;
transitionTarget = 'suspended';
- const promise = audioContext.suspend();
-
- promise.finally(() => {
- if (transitionTarget === 'suspended') {
+ // Chromium can freeze the audio clock if suspend runs before an
+ // in-flight resume settles, while its reported state is still suspended.
+ // A newer playback request must cancel this deferred suspension.
+ const promise = pendingResume
+ ? pendingResume
+ .catch(() => {})
+ .then(() => {
+ if (transitionId !== id) {
+ return;
+ }
+
+ return audioContext.suspend();
+ })
+ : audioContext.suspend();
+
+ return promise.finally(() => {
+ if (transitionId === id) {
transitionTarget = null;
}
});
-
- return promise;
};
return {
diff --git a/packages/player/src/test/audio-context-resume.test.tsx b/packages/player/src/test/audio-context-resume.test.tsx
index 788bf5ddac..2b8c06a373 100644
--- a/packages/player/src/test/audio-context-resume.test.tsx
+++ b/packages/player/src/test/audio-context-resume.test.tsx
@@ -18,6 +18,8 @@ afterEach(() => {
let createdAudioContexts = 0;
let resumeCalls = 0;
+let suspendCalls = 0;
+let pendingResumes: (() => void)[] = [];
let animationFrameId = 0;
let animationFrames = new Map<number, FrameRequestCallback>();
@@ -52,13 +54,19 @@ class FrozenAudioContext {
}
suspend() {
+ suspendCalls++;
this.state = 'suspended';
return Promise.resolve();
}
resume() {
resumeCalls++;
- return new Promise<void>(() => undefined);
+ return new Promise<void>((resolve) => {
+ pendingResumes.push(() => {
+ this.state = 'running';
+ resolve();
+ });
+ });
}
getOutputTimestamp() {
@@ -84,6 +92,8 @@ const AudioComposition = () => {
beforeEach(() => {
createdAudioContexts = 0;
resumeCalls = 0;
+ suspendCalls = 0;
+ pendingResumes = [];
animationFrameId = 0;
animationFrames = new Map<number, FrameRequestCallback>();
globalThis.AudioContext =
@@ -140,12 +150,47 @@ test.serial(
expect(resumeCalls).toBe(1);
expect(animationFrames.size).toBe(1);
+ const suspendsBeforePause = suspendCalls;
await act(async () => {
playerRef.current?.pause();
await Promise.resolve();
});
expect(playerRef.current?.isPlaying()).toBe(false);
expect(animationFrames.size).toBe(0);
+ // A pending native resume must settle before it is safe to suspend.
+ expect(suspendCalls).toBe(suspendsBeforePause);
+ await act(async () => {
+ pendingResumes.shift()!();
+ await Promise.resolve();
+ });
+ expect(suspendCalls).toBe(suspendsBeforePause + 1);
+
+ await act(async () => {
+ playerRef.current?.play();
+ await Promise.resolve();
+ });
+ await act(async () => {
+ playerRef.current?.pause();
+ await Promise.resolve();
+ });
+ await act(async () => {
+ playerRef.current?.play();
+ await Promise.resolve();
+ });
+ // Resume is still called synchronously; a newer Play cancels the
+ // deferred suspension belonging to the preceding Pause.
+ expect(resumeCalls).toBe(3);
+ await act(async () => {
+ pendingResumes.splice(0).forEach((resolve) => resolve());
+ await Promise.resolve();
+ });
+ expect(suspendCalls).toBe(suspendsBeforePause + 1);
+ expect(playerRef.current?.isPlaying()).toBe(true);
+ await act(async () => {
+ playerRef.current?.pause();
+ await Promise.resolve();
+ });
+ expect(suspendCalls).toBe(suspendsBeforePause + 2);
},
);