fix(player): release object urls (#1121)

This commit is contained in:
pan93412 2021-12-24 17:19:16 +08:00 committed by GitHub
parent d049eb4903
commit 836b7d0ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,6 +38,14 @@ export default class {
this._personalFMTrack = { id: 0 }; // 私人FM当前歌曲 this._personalFMTrack = { id: 0 }; // 私人FM当前歌曲
this._personalFMNextTrack = { 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) // howler (https://github.com/goldfire/howler.js)
this._howler = null; this._howler = null;
Object.defineProperty(this, '_howler', { Object.defineProperty(this, '_howler', {
@ -247,7 +255,21 @@ export default class {
_getAudioSourceFromCache(id) { _getAudioSourceFromCache(id) {
return getTrackSource(id).then(t => { return getTrackSource(id).then(t => {
if (!t) return null; if (!t) return null;
// Create a new object URL.
const source = URL.createObjectURL(new Blob([t.source])); 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; return source;
}); });
} }