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

211
Vistas
How to cache a webaudio object properly?

I'm developing a game using javascript and other web technologies. In it, there's a game mode that is basically a tower defense, in which multiple objects may need to make use of the same audio file(.ogg) at the same time. Loading a file and creating a new webaudio for each one of those lags it too much, even if I attempt to stream it instead of a simple sync read, and if I create and save a webaudio in a variable to use multiple times, each time its playing and there is a new request to play said audio, the one that was playing will stop to allow for the new one to play(so, with enough of those, nothing plays at all).

With those issues, I decided to make copies of said webaudio object each time it was gonna be played, but its not only slow to do so, but also creates a minor memory leak(at least the way I did it).

How can I properly cache a webaudio for re-use? Consider that I'm pretty sure I'll need a new one each time because each audio has a position, and thus each of them will play differently, based on player position in relation to object that is playing the audio

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You tagged your question with web-audio-api, but from the body of this question, it seems you are using an HTMLMediaElement <audio> instead of the Web Audio API.

So I'll invite you to do the transition to that Web Audio API.

From there you'll be able to decode once your audio file, keep only once the decoded data as an AudioBuffer, and create many readers that will all hook to that one and only AudioBuffer, without eating any more memory.

const btn = document.querySelector("button")
const context = new AudioContext();
// a GainNode to control the output volume of our audio
const volumeNode = context.createGain();
volumeNode.gain.value = 0.5; // from 0 to 1
volumeNode.connect(context.destination);

fetch("https://dl.dropboxusercontent.com/s/agepbh2agnduknz/camera.mp3")
  // get the resource as an ArrayBuffer
  .then((resp) => resp.arrayBuffer())
  // decode the Audio data from this resource
  .then((buffer) => context.decodeAudioData(buffer))
  // now we have our AudioBuffer object, ready to be played
  .then((audioBuffer) => {
    btn.onclick = (evt) => {
      // allowing an AudioContext to make noise
      // must be required from an user-gesture
      if (context.status === "suspended") {
        context.resume();
      }
      // a very light player object
      const source = context.createBufferSource();
      // a simple pointer to the big AudioBuffer (no copy)
      source.buffer = audioBuffer;
      // connect to our volume node, itself connected to audio output
      source.connect(volumeNode);
      // start playing now
      source.start(0);
    };
    // now you can spam the button!
    btn.disabled = false;
  })
  .catch(console.error);
<button disabled>play</button>

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