Merge pull request #127 from UniversityRadioYork/mstratford-ram-leak
Fix ram leak caused by not destroying WaveSurfers on reload.
This commit is contained in:
commit
a1af53c2bf
2 changed files with 16 additions and 1 deletions
|
@ -149,6 +149,11 @@ class Player extends ((PlayerEmitter as unknown) as { new (): EventEmitter }) {
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
// Let wavesurfer remove the old media, otherwise ram leak!
|
||||||
|
this.wavesurfer.destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LevelsSource = "mic-precomp" | "mic-final" | "master";
|
export type LevelsSource = "mic-precomp" | "mic-final" | "master";
|
||||||
|
@ -270,6 +275,16 @@ export class AudioEngine extends ((EngineEmitter as unknown) as {
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wavesurfer needs cleanup to remove the old audio mediaelements. Memory leak!
|
||||||
|
public destroyPlayerIfExists(number: number) {
|
||||||
|
const existingPlayer = this.players[number];
|
||||||
|
if (existingPlayer !== undefined) {
|
||||||
|
// already a player setup. Clean it.
|
||||||
|
existingPlayer.cleanup();
|
||||||
|
}
|
||||||
|
this.players[number] = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
async openMic(deviceId: string) {
|
async openMic(deviceId: string) {
|
||||||
if (this.micSource !== null && this.micMedia !== null) {
|
if (this.micSource !== null && this.micMedia !== null) {
|
||||||
this.micMedia.getAudioTracks()[0].stop();
|
this.micMedia.getAudioTracks()[0].stop();
|
||||||
|
|
|
@ -298,7 +298,7 @@ export const load = (
|
||||||
if (waveform == null) {
|
if (waveform == null) {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
}
|
}
|
||||||
waveform.innerHTML = ""; // clear previous (ghost) wavesurfer
|
audioEngine.destroyPlayerIfExists(player); // clear previous (ghost) wavesurfer and it's media elements.
|
||||||
// wavesurfer also sets the background white, remove for progress bar to work.
|
// wavesurfer also sets the background white, remove for progress bar to work.
|
||||||
waveform.style.removeProperty("background");
|
waveform.style.removeProperty("background");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue