From 836b7d0ce9ef21137fa78ffd9df02114cab2210f Mon Sep 17 00:00:00 2001 From: pan93412 Date: Fri, 24 Dec 2021 17:19:16 +0800 Subject: [PATCH] fix(player): release object urls (#1121) --- src/utils/Player.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/utils/Player.js b/src/utils/Player.js index f1c7f4f..c6b633e 100644 --- a/src/utils/Player.js +++ b/src/utils/Player.js @@ -38,6 +38,14 @@ export default class { this._personalFMTrack = { id: 0 }; // 私人FM当前歌曲 this._personalFMNextTrack = { id: 0 }; // 私人FM下一首歌曲信息(为了快速加载下一首) + /** + * The blob records for cleanup. + * + * @private + * @type {string[]} + */ + this.createdBlobRecords = []; + // howler (https://github.com/goldfire/howler.js) this._howler = null; Object.defineProperty(this, '_howler', { @@ -247,7 +255,21 @@ export default class { _getAudioSourceFromCache(id) { return getTrackSource(id).then(t => { if (!t) return null; + + // Create a new object URL. const source = URL.createObjectURL(new Blob([t.source])); + + // Clean up the previous object URLs since we've created a new one. + // Revoke object URLs can release the memory taken by a Blob, + // which occupied a large proportion of memory. + for (const url in this.createdBlobRecords) { + URL.revokeObjectURL(url); + } + + // Then, we replace the createBlobRecords with new one with + // our newly created object URL. + this.createdBlobRecords = [source]; + return source; }); }