S
S
Semro2018-06-30 16:35:09
JavaScript
Semro, 2018-06-30 16:35:09

Why is the buffer reset to zero after the decodeAudioData method has run?

There is the following code:

function play(arraybuffer) {
  let audioCtx = new AudioContext();
  let audioData = arraybuffer;
  audioCtx.decodeAudioData(
    audioData,
    function (buffer) {
      let smp = audioCtx.createBufferSource();
      smp.buffer = buffer;
      smp.connect(audioCtx.destination);
      smp.start(0);
    },
    function (e) {
      console.log( "Error with decoding audio data: " + e.err );
    }
  );
}
console.log(sound); // Выведет ArrayBuffer(106488) {}
play(sound);
console.log(sound); // Выведет ArrayBuffer(0) {}

The buffer is written to the sound variable from the socket received from the server. It is necessary to produce this sound several times, but how to do it if the variable is reset to zero? I would not really like to download data from the server again.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
floydback, 2020-05-07
@floydback

Context creation and decoding need to be done once. Save the audio buffer to the variable buffer . But every time you start play, do only what is necessary

play () {
  source = context.createBufferSource()
  source.buffer = buffer
  source.connect(destination)
  source.start()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question