audio-loader icon indicating copy to clipboard operation
audio-loader copied to clipboard

Loading base64 source

Open peterbe opened this issue 6 years ago • 0 comments

This works:

import play from "audio-play";
import load from "audio-loader";

load("./sounds/coin.mp3").then(audioBuffer => {
  play(audioBuffer);
});

But I want to encode the AudioBuffer to a string (so it can be stored in localStorage next time) and back again.

This does NOT work:


// Copied from  audio-loader/test/support/utils.js
function arrToBase64Audio(arr) {
  var data = new Buffer(arr).toString("base64");
  return "data:audio/mp3;base64," + data;
}

load("./sounds/coin.mp3").then(audioBuffer => {
  console.log(audioBuffer);
  const asString = arrToBase64Audio(audioBuffer);
  console.log(asString);
  console.log(asString.length);
  load(asString).then(ab => {
    //console.log(ab);
    play(ab);
  });
});

It says:

The buffer passed to decodeAudioData contains an unknown content type.
screen shot 2018-05-07 at 5 01 07 pm *Firefox* screen shot 2018-05-07 at 5 02 22 pm *Chrome*

peterbe avatar May 07 '18 21:05 peterbe