Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting audio file to base64 using javascript

I want to convert audio file into base64 using Javascript only.

We can convert images into base64 using canvas. But how can we convert audio files.

Any help will be grateful.

like image 825
venky Avatar asked Jul 01 '15 12:07

venky


People also ask

Can we convert file to Base64?

Convert Files to Base64Just select your file or drag & drop it below, press the Convert to Base64 button, and you'll get a base64 string. Press a button – get base64. No ads, nonsense, or garbage. The input file can also be an mp3 or mp4.

What is Base64 in Javascript?

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.


1 Answers

you can give the below code a try, it uses btoa

function getData(audioFile, callback) {
    var reader = new FileReader();
    reader.onload = function(event) {
        var data = event.target.result.split(',')
         , decodedImageData = btoa(data[1]);                    // the actual conversion of data from binary to base64 format
        callback(decodedImageData);        
    };
    reader.readAsDataURL(audioFile);
}
like image 170
mido Avatar answered Sep 29 '22 15:09

mido