Skip to content

Commit 55badf2

Browse files
committed
fix(core): a handed-over element sounds again when playback stops
1 parent f865165 commit 55badf2

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,19 @@ describe("WebAudioTransport", () => {
209209
expect(node.connect).not.toHaveBeenCalled();
210210
});
211211

212+
it("sounds a handed-over element through the output again once playback stops", async () => {
213+
const { transport, mock, gen } = setupTransport(100);
214+
const node = { connect: vi.fn(), disconnect: vi.fn() };
215+
mock.ctx.createMediaElementSource = vi.fn(() => node) as never;
216+
await transport.scheduleMediaElementPlayback(mockEl, 0, 0, 0, 1, gen, 1);
217+
transport.releaseRouted(mockEl);
218+
node.connect.mockClear();
219+
220+
transport.stopAll();
221+
222+
expect(node.connect).toHaveBeenCalledWith(mock.ctx.destination);
223+
});
224+
212225
it("creates one MediaElementAudioSourceNode per element and context", async () => {
213226
const { transport, mock } = setupTransport(100);
214227

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class WebAudioTransport {
240240
private _pendingDecodeBytes = new Map<string, number>();
241241
private _failedSrcs = new Set<string>();
242242
private _mediaElementSources = new WeakMap<HTMLMediaElement, MediaElementAudioSourceNode>();
243-
private _handedOver = new WeakSet<HTMLMediaElement>();
243+
private _handedOver = new Set<HTMLMediaElement>();
244244
private _activeSources: ScheduledSource[] = [];
245245
private _masterGain: GainNode | null = null;
246246
/** Where the master bus chain lands; the program meter taps here. */
@@ -1052,8 +1052,7 @@ export class WebAudioTransport {
10521052
}
10531053
}
10541054

1055-
/** Drops the routed element's source once a decoded buffer plays in its place. A captured element has no native
1056-
* output, so left unconnected it is silent; WebKit sounds it through the destination even while muted. */
1055+
/** Drops the routed element's source once a decoded buffer plays in its place; it stays silent until a stop. */
10571056
releaseRouted(el: HTMLMediaElement): void {
10581057
const routed = this._activeSources.find((s) => s.el === el && s.sourceKind === "media-element");
10591058
if (!routed) return;
@@ -1133,11 +1132,23 @@ export class WebAudioTransport {
11331132
stopAll(): void {
11341133
for (const source of this._activeSources) this.dispose(source);
11351134
this._activeSources = [];
1135+
this.reconnectHandedOver();
11361136
this._paused = true;
11371137
this._playGeneration += 1;
11381138
this.rest();
11391139
}
11401140

1141+
private reconnectHandedOver(): void {
1142+
for (const el of this._handedOver) {
1143+
try {
1144+
if (this._ctx) this._mediaElementSources.get(el)?.connect(this._ctx.destination);
1145+
} catch (err) {
1146+
swallow("webAudioTransport.reconnectHandedOver", err);
1147+
}
1148+
}
1149+
this._handedOver.clear();
1150+
}
1151+
11411152
/** Stops what plays from media gone from the page, without sending it to the destination as a stop does. */
11421153
releaseDetached(): void {
11431154
this._activeSources = this._activeSources.filter((source) => {

0 commit comments

Comments
 (0)