If we're talking about the browser, yes, it is. You can use URL.createObjectURL()
on your file
, use that as the src
for an <audio>
element you create, wait for the element's canplaythrough
event, then read the duration
property.
As a matter of fact, that code is available in my recent answer here (and reproduced below); it should be rather easy to integrate in your code.
function computeLength(file) {
return new Promise((resolve) => {
var objectURL = URL.createObjectURL(file);
var mySound = new Audio([objectURL]);
mySound.addEventListener(
"canplaythrough",
() => {
URL.revokeObjectURL(objectURL);
resolve({
file,
duration: mySound.duration
});
},
false,
);
});
}