7

I am using the MediaRecorder API to record audio on my page.

I need to convert this audio to base64.

Have a look at this example.

Each time new data is available, it pushes that data to an array, like this:

function handleDataAvailable(event) {
  if (event.data && event.data.size > 0) {
    recordedBlobs.push(event.data);
  }
}

Then, it combines all that data like this:

var superBuffer = new Blob(recordedBlobs, {type: 'video/webm'});

So how would I convert this superBuffer to base64?

Luke
  • 2,038
  • 3
  • 22
  • 46

1 Answers1

15

You can do this using FileReader Object.

var reader = new window.FileReader();
reader.readAsDataURL(superBuffer); 
reader.onloadend = function() {
   base64 = reader.result;
   base64 = base64.split(',')[1];
   console.log(base64 );
}

Answer referred from Convert blob to base64.

Read more about FileReader for better understanding.

Community
  • 1
  • 1
vinoth h
  • 511
  • 3
  • 11
  • This answer is incredibly useful specially if you are trying to use Node's FS api to write the base64 encoded blobs into a file. Not doing the `.split(',')[1]` part causes corrupt webm files to be generated. – Sepehr Jul 23 '18 at 08:23