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

162
Vistas
Attribute for duration on <audio> tags

When working with <audio> tags I usually set the preload="metadata" in order to reduce bandwidth. Still this triggers a request on page load because the browser retrieves the duration of the file.

Is there a way to set the duration to an <audio> tag via an attribute? That way no request at all would be needed for the browser to display the duration prior to starting it. Something like:

<audio src="myfile.mp3" duration="600" preload="none">

Whereby 600 would be seconds.

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

0

This can be achieved by setting audio.src with bytes for an "empty" audio file. For example, an RIFF/WAV file could be created for your required duration, and the browser renders the duration time in the UI.

document.querySelectorAll('audio[data-duration]').forEach(initAudio)

function initAudio(audio) {
  // 0s 8-bit Mono Linear PCM 8000Hz WAV header
  const sampleRate = 8000
  const wavHeader = new Uint32Array([1179011410, 36, 1163280727, 544501094, 16, 65537, sampleRate, sampleRate, 524289, 1635017060, 0])

  const { duration, url } = audio.dataset

  // PCM8 WAV data
  const wavData = new Uint8Array(Math.ceil(duration * sampleRate))
  const dataSize = wavData.byteLength

  // set sizes
  wavHeader.set([dataSize + 36], 1)
  wavHeader.set([dataSize], 10)

  audio.src = URL.createObjectURL(new Blob([wavHeader, wavData]))

  // swap audio with url to play
  audio.addEventListener('play', () => {
    audio.src = url
    audio.play()
  }, { once: true })
}
<audio controls
  data-duration="15.13"
  data-url="https://opus-bitrates.anthum.com/audio/music-96.opus" />

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