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

181
Visualizações
How can I programmatically determine when an JavaScript AudioContext AudioBufferSource is done playing?

I am playing .MP3 files with the Web Audio API, and I am having trouble programmatically determining when the song is finished playing.

This is what I have:

var request = new XMLHttpRequest();

audioContext = new AudioContext();

source = audioContext.createBufferSource();
source.connect(audioContext.destination);

request.open('GET',url,true);
request.responseType='arraybuffer';
request.onload = function() {
    audioContext.decodeAudioData(request.response).then(function(buffer) {
        source.buffer = buffer;
        source.start(0);
    });
}   
request.send();

This works well; I can start, stop, pause, and unpause a song with no problems.

What I want to do, however, is when a song stops playing upon completion (i.e., I have not called audioContext.close()), I want to call another function to set a message for the user. I have looked for some sort of callback I could leverage, I have tried the finally() of the promise provided by the audioContext.decodeAudioData() provides, but I haven't been able to figure this out.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Handle the ended event of your audioContext. See AudioScheduledSourceNode: ended event for reference.

source.addEventListener('ended', () => {
   // done!
});

Or use the onended property.

source.onended = () => {
  // done!
}
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