How can I get the file size of a non-DOM created audio element?
I thought I could use the HTML5 File API – as per Client Checking file size using HTML5? – but it doesn't seem to be working for elements not in the DOM.
I've created an example below – see the line console.log(audio_file.files[0].size);
, which gives an error of:
TypeError: audio_file.files is undefined
// Create a non-dom allocated Audio element
var audio_file = document.createElement('audio');
// Define the URL of the MP3 audio file
audio_file.src = "https://cdn.plyr.io/static/demo/Kishi_Bashi_-_It_All_Began_With_a_Burst.mp3";
// Once the metadata has been loaded, display the duration in the console
audio_file.addEventListener('loadedmetadata', function(){
// Obtain the duration in seconds of the audio file
var duration = audio_file.duration;
const duration_hhmmss = new Date(duration * 1000).toISOString().substr(11, 8);
console.log("The duration of the song is of: " + duration_hhmmss);
console.log(audio_file.files[0].size);
},false);