converting audio file to base64 using javascript

佐手、 提交于 2019-12-28 18:14:09

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/31161897/converting-audio-file-to-base64-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!