Skip to content

Commit 9a238aa

Browse files
authored
Dispose sound instances when they stop playing (#17409)
1 parent 8588c38 commit 9a238aa

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/dev/core/src/AudioV2/abstractAudio/abstractSound.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,7 @@ export abstract class AbstractSound extends AbstractSoundSource {
252252
this._state = SoundState.Stopped;
253253
this.onEndedObservable.notifyObservers(this);
254254
}
255+
256+
instance.dispose();
255257
};
256258
}

packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,11 @@ class _WebAudioStaticSoundInstance extends _StaticSoundInstance implements IWebA
358358
const restart = this._state === SoundState.Starting || this._state === SoundState.Started;
359359

360360
if (restart) {
361-
this.stop();
361+
// Stop source node without sending `onEndedObservable` so instance's `dispose` function is not called.
362+
const sourceNode = this._sourceNode;
362363
this._deinitSourceNode();
364+
sourceNode?.stop();
365+
this._state = SoundState.Stopped;
363366
}
364367

365368
this._options.startOffset = value;
@@ -467,12 +470,13 @@ class _WebAudioStaticSoundInstance extends _StaticSoundInstance implements IWebA
467470
return;
468471
}
469472

470-
this._setState(SoundState.Stopped);
471-
472473
const engineStopTime = this.engine.currentTime + (options.waitTime ?? 0);
473474
this._sourceNode?.stop(engineStopTime);
474475

475-
this.engine.stateChangedObservable.removeCallback(this._onEngineStateChanged);
476+
if (options.waitTime === undefined || options.waitTime <= 0) {
477+
this._setState(SoundState.Stopped);
478+
this.engine.stateChangedObservable.removeCallback(this._onEngineStateChanged);
479+
}
476480
}
477481

478482
protected override _connect(node: AbstractAudioNode): boolean {

0 commit comments

Comments
 (0)