GatesKennedy
GatesKennedy

Reputation: 725

Find the time duration/length of an audio file uploaded to a React application

I have a React web application (create-react-app) that takes song uploads using react-hook-forms and sends it to my Node/express server using axios. I'd like to determine the length of the song and store this value in my postgresql database before sending the audio file to be stored in an AWS S3 bucket.

Here is an example of an mp3 console log

Thank you!

Upvotes: 0

Views: 3086

Answers (1)

M-N
M-N

Reputation: 633

You can use the Web Audio API. Once you've got an audio context object, you can use the decodeAudioData method to do what you're trying to do. That method takes a callback function that takes one argument: an AudioBuffer that has a read-only duration property. That is,

audioContext.decodeAudioData(audioData, (buffer) => {
    const duration = buffer.duration;
    // do stuff with the duration
});

Upvotes: 3

Related Questions