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

134
Views
Is it possible to play the same audio on two audio output devices simultaneously

I'm trying to make a script that gets the audio from an audio element and plays it on headset and laptop speakers at he same time. Is this possible?

This is the code that I wrote so far:

const outputs = (await navigator.mediaDevices.enumerateDevices()).filter(({ kind, deviceId }) => kind === 'audiooutput' && deviceId !== 'default' && deviceId !== 'communications' );
var players = [];
document.querySelectorAll('audio').forEach((y)=>{
    players.push([]); let l = players.length-1;
    outputs.forEach((x)=>{
        if(players[l].length===0) players[l].push(y);
        else {
            let p = y.cloneNode();
            p.srcObject = players[l][0].srcObject.clone();
            players[l].push(p);
        }
    });
})
players.forEach((a)=>{
    a.forEach((o, i)=>{
        o.setSinkId(outputs[i].deviceId);
        o.play();
    })
})

The issue with this code is that it makes the audio only play on the other speaker instead of playing it on both.

Note that the window has access to mic so I can see all the devices from navigator.mediaDevices.enumerateDevices(). The script is intended to work mainly on Edge and Chrome.

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

0

I found a solution!
Since I wasn't able to play sound on multiple devices at once in the same window I managed to get this done by creating iframes that contain audio elements. Then I set to all of those audio elements the same source and I set them to output audio a specific device and I sync the play/stop with event listeners.

This works because iframes are separated by the main window. Actually I think they get their own process (on chromium browsers).

My code looks like this: (note I'm not using static sources, but srcObjects)

navigator.mediaDevices.getUserMedia({audio: true}).then(s=>{
    s.getTracks().forEach(x=>x.stop()); //stop mic use because we need only outputs
    navigator.mediaDevices.enumerateDevices().then(o=>{
        const outputs = o.filter(({ kind, deviceId }) => kind === 'audiooutput' && deviceId !== 'default' && deviceId !== 'communications');

        let audioSrc = getAudioSrc(), players = [];

        audioSrc.src.pause(); // Pause source to start audio in sync ?
        audioSrc.src.addEventListener("pause", () => players.forEach(x=>x.pause()));
        audioSrc.src.addEventListener("play", () => players.forEach(x=>x.play()));

        outputs.forEach((x)=>{
            let ifrm = makeIFrame(), audioEl = makeAudio(ifrm.document, audioSrc.s, x.deviceId);
            players.push(audioEl);
        });
    });
}).catch(e=>console.log(e));
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!