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

139
Vistas
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 Respuestas
Responde la pregunta

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 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