Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Audio Constructor in Javascript is returning NaN

I was trying to to get duration of audio files using creating audio Constructor in a for Each Loop but audioElement.duration is returning NaN only. Here's my code.

let audioArray = [
    '1.mp3',
    '2.mp3',
    '3.mp3',
    '4.mp3'
]

let audioElement;

audioArray.forEach((element)=>{
    audioElement = new Audio(element);
    console.log(audioElement.duration);
})

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The browser won't know the duration until enough of the file has been downloaded, so you need to wait for the correct event.

See this snippet from https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement

audioElement.addEventListener('loadeddata', () => {
  let duration = audioElement.duration;
  // The duration variable now holds the duration (in seconds) of the audio clip
})

In your example, to log out the durations you would do this:

let audioArray = [
    '1.mp3',
    '2.mp3',
    '3.mp3',
    '4.mp3'
]
audioArray.forEach((element)=>{
    let audioElement = new Audio(element);
    audioElement.addEventListener('loadeddata', () => {
      console.log(`${element} duration:`, audioElement.duration);
    });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda