3

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)
};
Jacob Hornbeck
  • 398
  • 2
  • 19
Nasa
  • 43
  • 1
  • 7
  • I don't get the link between your code and your question. If you have a dataURI coming from your database, the dataURI you have is already a representation of the full mp3 file. You can directly add this as the source (`src`) of an ` – Kaiido Apr 30 '16 at 09:09
  • and if you actually have a binary string, you just have to add the dataURI header `'data:audio/mp3;base64,'` to the `btoa()`of your result (`var dataURI = 'data:audio/mp3;base64,'+btoa(binaryString)`) – Kaiido Apr 30 '16 at 09:20

1 Answers1

1

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

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

Community
  • 1
  • 1
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • ,thx, i converted using window.atob i am getting binary data, how i can covert back to Mp3,so i can play the file and save it. – Nasa Apr 29 '16 at 11:18
  • The data you have now in the buffer is real mp3 data. Use the link in my answer to create the player and use the data you have as a datauri in src. – Charlie Apr 29 '16 at 11:20
  • i want to save in local folder has mp3 file with name,i have binary data, want to play it using JavaScript,i am using File Api – Nasa Apr 29 '16 at 14:37