Skip to content
Advertisement

Decode Base 64 audio file Mp3 into playable Mp3

I am converting the audio MP3 file and storing it as base64 in database using WEB API, now I am fetching the base64 file using ajax and I am getting the base64, the problem is how can I convert the base64 file back to mp3 file and play it using JavaScript.

This if for demo I am using input file, I am getting base64 file from server

<input type='file' onchange='openFile(event)' id="EdituserProfileImage">
var fileName;
var filetype;
var filesize;
var VoiceBase64;
var openFile = function (event) {
    var input = event.target;
    fileName = input.files[0].name;
    filetype = input.files[0].type;
    filesize = input.files[0].size;
    console.log(input);
    console.log(fileName);
    var reader = new FileReader();
    reader.onload = function (evt) {
        var voiceInBinay = evt.target.result;
        VoiceBase64 = btoa(voiceInBinay);
        contvertBase64toBinaray(VoiceBase64);
    };
    reader.readAsBinaryString(input.files[0]);
};

This function “contvertBase64toBinaray” using for converting base64 to Binary, I have binary file, need to save as mp3 from this below binary

function contvertBase64toBinaray(VoiceBase64) {
    var audiofile = atob(VoiceBase64)
};

Advertisement

Answer

Use window.atob function to decode your base 64 data.

This question shows you how you can play the mp3 in JS.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement