Skip to content

Commit aaf0971

Browse files
committed
improve disconnect feature
1 parent f33e75c commit aaf0971

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/library/audio.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,16 @@ export class PlayerAudio {
221221

222222
// if media element source also destroy the media element? (for each song?)
223223
songsQueue.forEach((sound) => {
224-
if (sound.sourceNode instanceof MediaElementAudioSourceNode) {
225-
if (typeof sound.sourceNode.mediaElement !== 'undefined') {
226-
sound.sourceNode.mediaElement.remove();
224+
if (sound.sourceNode !== null) {
225+
if (sound.sourceNode instanceof MediaElementAudioSourceNode) {
226+
if (typeof sound.sourceNode.mediaElement !== 'undefined') {
227+
sound.sourceNode.mediaElement.remove();
228+
}
227229
}
230+
// disconnect no matter if AudioBufferSourceNode
231+
// or MediaElementAudioSourceNode
232+
sound.sourceNode.disconnect();
228233
}
229-
sound.sourceNode.disconnect();
230234
});
231235

232236
this._disconnectPlayerGainNode();
@@ -351,8 +355,10 @@ export class PlayerAudio {
351355

352356
protected _disconnectPlayerGainNode(): void {
353357

354-
this._audioNodes.gainNode.disconnect();
355-
358+
if (this._audioNodes.gainNode !== null) {
359+
this._audioNodes.gainNode.disconnect();
360+
}
361+
356362
this._audioNodes.gainNode = null;
357363

358364
}
@@ -368,18 +374,12 @@ export class PlayerAudio {
368374

369375
}
370376

371-
public async disconnectSound(sound: ISound): Promise<void> {
372-
373-
if (sound.gainNode !== null) {
374-
//sound.gainNode.disconnect();
375-
} else {
376-
throw new PlayerError('can\'t destroy as no source node in sound');
377-
}
377+
public async cleanUpAudiBufferSourceNode(sound: ISound): Promise<void> {
378378

379379
if (sound.sourceNode instanceof AudioBufferSourceNode) {
380-
// the audio buffer source node we set it to undefined, to let it get destroyed
381-
// by the garbage collector as you can't reuse an audio buffer source node
382-
// (after it got stopped) as specified in the specs
380+
// the audio buffer source node we set it to null, so that it gets destroyed
381+
// by the garbage collector (as you can't reuse an audio buffer source node,
382+
// after it got stopped) as specified in the specs
383383
sound.sourceNode = null;
384384
}
385385

src/library/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ export class PlayerCore {
11351135
sound.sourceNode.mediaElement.pause();
11361136
}
11371137

1138-
this._playerAudio.disconnectSound(sound);
1138+
this._playerAudio.cleanUpAudiBufferSourceNode(sound);
11391139

11401140
// state is now stopped
11411141
sound.state = soundState;

0 commit comments

Comments
 (0)