Muhammed K. Sayed
Muhammed K. Sayed

Reputation: 11

how can i get audio's duration without getting loaded in js

let's imagine if i'm building a music player okay ? actually i did it but i faced a problem when i try to get list of the songs and get their duration i must create audio element for each song and it waste alot of data so there's in a pure js code or another without writing duration statically ? this is my code

    songs.map((song) => {
        if(!song.hasOwnProperty("duration")){
            let aud = new Audio();
            aud.src = `audios/${song.audioName}.mp3`
            aud.addEventListener('loadedmetadata',() => {
                let durationAud = aud.duration;
                song.duration = durationAud;
            })
        }
    })

}

Upvotes: 0

Views: 978

Answers (1)

Cyrus
Cyrus

Reputation: 691

You would solve this creating a row in a database with your audio file's metadata. Then, you would request that data on your frontend with an AJAX call.

Upvotes: 2

Related Questions