Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda