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

138
Views
Multiple Audio elements interfering on the same page

I have a webapp with multiple audio bubbles, but playing one audio bubble plays audio from the incorrect audio bubble. I'm using: this.audio = new Audio(url). They are interfering in some way. How to solve that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ensure all audio objects are accessible to each other and pause them before you start one.

const audioPromises = [
  audioFromUrl('https://opus-bitrates.anthum.com/audio/hyper/music-96.opus'), 
  audioFromUrl('https://opus-bitrates.anthum.com/audio/music-96.opus'),
]

document.querySelectorAll('.play').forEach(el => el.addEventListener('click', () => {
  play(parseInt(el.dataset.idx))
}))

document.querySelectorAll('.stop').forEach(el => el.addEventListener('click', stopAll))

async function play(idx) {
  const audio = await audioPromises[idx]
  await stopAll()
  audio.play()
}

function stopAll() {
  return Promise.all(audioPromises)
    .then(audios => audios.map(audio => audio.pause()))
}


async function audioFromUrl(url) {
  const arrayBuffer = await (await fetch(url)).arrayBuffer()
  const audio = new Audio()
  audio.src = URL.createObjectURL(
    new Blob([arrayBuffer])
  )
  return audio
}
<button class="play" data-idx="0">Play 1</button>
<button class="play" data-idx="1">Play 2</button>
<br /><br />
<button class="stop" data-idx="1">Stop All</button>

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!