diff --git a/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts b/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts index 2c20a4642..f3c4bdc57 100644 --- a/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts +++ b/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts @@ -240,6 +240,40 @@ describe("MediaElementPlaybackSource native playback", () => { } }); + test("a recovery reload does not reject an in-flight play for the same resume intent", async () => { + jest.useFakeTimers(); + const browser = installBrowser(); + const source = new MediaElementPlaybackSource(null, "native"); + try { + const audio = browser.audio(); + await startNativeStream(source, audio, "https://radio.example/live.mp3"); + source.pause(); + const pending = Promise.withResolvers(); + audio.play = () => { + audio.paused = false; + return pending.promise; + }; + const result = source.play().catch((error: unknown) => error); + audio.emit("waiting"); + jest.advanceTimersByTime(6000); + jest.advanceTimersByTime(0); + await flushMicrotasks(); + // Recovery reattaches through loadAttachedSource without going through + // load(), so the browser rejects the earlier play() with this AbortError. + pending.reject( + new DOMException( + "The play() request was interrupted by a new load request.", + "AbortError" + ) + ); + expect(await result).toBeUndefined(); + expect(source.status).not.toBe("error"); + } finally { + source.cleanup(); + browser.restore(); + } + }); + test.each(["AbortError", "NotAllowedError", "NotSupportedError"])( "an active play still reports %s", async (name) => { diff --git a/apps/radio/src/lib/audio/playback/media-element-playback-source.ts b/apps/radio/src/lib/audio/playback/media-element-playback-source.ts index daf158e4b..dbbca0e73 100644 --- a/apps/radio/src/lib/audio/playback/media-element-playback-source.ts +++ b/apps/radio/src/lib/audio/playback/media-element-playback-source.ts @@ -70,11 +70,18 @@ export class MediaElementPlaybackSource implements PlaybackSource { this.setBuffering(false); this.callbacks.onError?.(error, recoveryPending); }, - reload: (input, generation) => this.loadAttachedSource(input, generation), + reload: (input, generation) => { + // Recovery reattaches without load(), so it must still retire any + // in-flight play() whose browser promise the reattach will abort. + // Keep shouldResumeAfterLoad so resume can continue afterward. + this.supersedeInFlightPlay(); + return this.loadAttachedSource(input, generation); + }, resume: async () => { this.audio.autoplay = true; + this.shouldResumeAfterLoad = true; if (this.audio.paused) { - await this.audio.play(); + await this.playForIntent(this.playbackIntent); } }, setState: (status, isBuffering) => { @@ -435,6 +442,10 @@ export class MediaElementPlaybackSource implements PlaybackSource { private cancelPendingPlaybackIntent(): void { this.shouldResumeAfterLoad = false; this.audio.autoplay = false; + this.supersedeInFlightPlay(); + } + + private supersedeInFlightPlay(): void { this.playbackIntent += 1; } @@ -442,8 +453,8 @@ export class MediaElementPlaybackSource implements PlaybackSource { try { await this.audio.play(); } catch (error) { - // A later pause, stop, load or play owns the element now. The browser - // can reject the old play promise after that action has completed. + // A later pause, stop, load, play or recovery reload owns the element + // now. The browser can reject the old play promise after that action. if (!this.isPlaybackIntentCanceled(playbackIntent)) { throw error; }