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

91
Visualizações
Play loaded audio file with Tone.js web audio framework

Hi I'm creating a simple upload page where user load their local files and play it. For hour, i'm not uploading to the server, just want to create a file input and then play the audio file with Tone.js if it's possibel.

I'm using to this test P5.js library and Tone.js audio framework, the code below:

let inputFile, bttPlayFile;
let files;

window.AudioContext = window.AudioContext || window.webkitAudioContext;
let context = new Tone.Context();
let source;

function setup() {
  
  inputFile = createFileInput(uploadTrack);
  bttPlayFile = createButton('play');
  bttPlayFile.mousePressed(tooglePlay);
}

function playSound(arraybuffer) {
  context.decodeAudioData(arraybuffer, function(buf) {
    source = new Tone.BufferSource();
    source.connect(context).toDestinattion();
    source.Tone.Buffer = buf;
    source.start(0);
  });
}

function selectedFile(evt) {   
  files = evt.target.files;
  playFile(files[0]);
}

function playFile(file) {
  let reader = new FileReader();
  reader.onload = function(e) {
    console.log(e.target.result);
    playSound(e.target.result);
  }
  reader.readAsArrayBuffer(file);
}

function uploadTrack() {
  inputFile.changed(selectedFile, false);
}

function tooglePlay() {
  Tone.Transport.start();
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.32/Tone.js" integrity="sha512-USKCQh+O8BX/a2K06xPNTwduhmQvN/m9FhkR7PRysCRlPoqIItl7Qz3xVTZC/oIHe6g5XvnLHDUgGpRMZZTmFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

At the moment, nothing happens and I want to understand why and how to proceed with this. Thanks!

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

0

If you open the console of your browser you should see a warning.

The AudioContext is "suspended". Invoke Tone.start() from a user action to start the audio.

I think it will work if you call Tone.start() in the click handler of your "play" button before you load and decode the selected audio file.

It's unrelated to your question but it should be safe to remove window.AudioContext = window.AudioContext || window.webkitAudioContext from your code. Tone.js uses standardized-audio-context internally which should handle this for you.

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