How to get audio duration inside the js loadmetadata event

Asked

Viewed 11 times

0

I’m trying to get the duration of the audio, when I try to access the duration variable it appears indefinite, someone knows some solution, I’ve tried using asynchronous promise nothing worked.

function getAllDuration(){
    let obj = audio_utility.audio_path;
    for (let i = 0; i < obj.length; i++){
        let tmp_str = [];
        for (let j = 0; j < obj[i].length; j++){
            //console.log(getDuration(obj[i][j]));
            let tmp_duration = getDuration(obj[i][j]);
            console.log(value);
        }
        //console.log(tmp_str);
    }
}
function getDuration(src){
    let duration = [], audio = new Audio(), dur;
    audio.src = src;
    audio.addEventListener('loadedmetadata', ()=>{
        duration.push(audio.duration);
        //console.log(duration); 
    });
    return duration;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.