Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!