1// Create a non-dom allocated Audio element
2var audio = document.createElement('audio');
3
4// Add a change event listener to the file input
5document.getElementById("fileinput").addEventListener('change', function(event){
6 var target = event.currentTarget;
7 var file = target.files[0];
8 var reader = new FileReader();
9
10 if (target.files && file) {
11 var reader = new FileReader();
12
13 reader.onload = function (e) {
14 audio.src = e.target.result;
15 audio.addEventListener('loadedmetadata', function(){
16 // Obtain the duration in seconds of the audio file (with milliseconds as well, a float value)
17 var duration = audio.duration;
18
19 // example 12.3234 seconds
20 console.log("The duration of the song is of: " + duration + " seconds");
21 // Alternatively, just display the integer value with
22 // parseInt(duration)
23 // 12 seconds
24 },false);
25 };
26
27 reader.readAsDataURL(file);
28 }
29}, false);