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

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

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