Skip to content

Commit b65ed03

Browse files
committed
fix(core): a browser-side audio suspend drops the preview's idle timer
1 parent b3fc944 commit b65ed03

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎packages/core/src/runtime/webAudioTransport.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ export class WebAudioTransport {
213213
}
214214

215215
private rest(): void {
216-
if (!this._ctx || this._ctx.state !== "running" || this._suspendPending) return;
217216
if (this._restTimer !== null) clearTimeout(this._restTimer);
217+
this._restTimer = null;
218+
if (!this._ctx || this._ctx.state !== "running" || this._suspendPending) return;
218219
this._restTimer = setTimeout(() => {
219220
this._restTimer = null;
220221
this.suspendIfIdle();

‎packages/core/src/runtime/webAudioTransportIdle.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,24 @@ describe("WebAudioTransport keeps its context suspended while nothing sounds", (
215215
expect(ctx.suspends).toBe(suspendsBefore + 1);
216216
});
217217

218+
it("drops its idle timer when the browser suspends the context itself", async () => {
219+
const { transport, ctx } = await startTransport();
220+
await play(transport, makeTrack());
221+
transport.stopAll();
222+
await vi.advanceTimersByTimeAsync(2000);
223+
// An interruption outside the transport (an OS or WebKit audio session) suspends the context.
224+
ctx.state = "suspended";
225+
ctx.onstatechange?.();
226+
let release!: () => void;
227+
resumeGate = new Promise((resolve) => (release = resolve));
228+
const playing = play(transport, makeTrack());
229+
await vi.advanceTimersByTimeAsync(9000);
230+
release();
231+
await playing;
232+
await vi.advanceTimersByTimeAsync(0);
233+
expect(ctx.state).toBe("running");
234+
});
235+
218236
it("anchors a decoded Play to the time read after the wake", async () => {
219237
const { transport } = await startTransport();
220238
let now = 10;

0 commit comments

Comments
 (0)