When fetching a blob for an audio file, as in the code sample below (no cached on disk but held in memory only), when the blob is no longer needed and the object URL is revoked, what is the proper way to clear the source from the audio element such that blob will be released in the garbage collection cycle?
I don’t want to get rid of the audio element itself and I don’t want to wait until a new valid audio source is selected. If the source is set to ""
then later audio.src
is the http address rather than “” or the blob url. (Unless I have a coding error elsewhere at the moment causing this.)
Is there a “clean” way to do this such that a console.log error is not generated and, perhaps, only the ’emptied” event is triggered/fired?
Is setting src = ""; audio.load();
the correct approach?
Thank you.
fetch(
`http://${url}/audio/${trackId}`,
{ method: 'GET', mode: 'same-origin', cache: 'no-store' }
)
.then( http => {
if ( !http.ok ) {
throw new TypeError('Network response was not ok.');
} else {
const contentType = http.headers.get('content-type');
if (!contentType || !contentType.includes('audio/mpeg')) {
throw new TypeError("Oops, we haven't got an audio file!");
}
}
return http.blob();
})
.then( result => {
let
objectURL = URL.createObjectURL( result )
;
audio_elem.src = objectURL;
})
;