Base64转amr音频文件

2022-12-23 09:53:52 浏览数 (1)

代码语言:javascript复制
function dataURL2Audio(fname, base64Str, callback) { //base64转amr文件
    var mv = base64Str || '';
    if (typeof callback != 'function' || mv == '') return;
    fname = fname || ((new Date()).getTime());
    fname = (fname.indexOf('.amr') == fname.length - 4) ? fname : fname   '.amr';
    var audioName = '_doc/audio/'   fname;
    plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
        fs.root.getFile(audioName, {
            create: true
        }, function (entry) {
            var fullPath = entry.fullPath;
            if (mui.os.android) {
                var Str = plus.android.importClass("java.lang.String");
                var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
                try {
                    var myatb, bstr;
                    var out = new FileOutputStream(fullPath);
                    var mystr = atob(mv);
                    var mlen = mystr.length;
                    var sstr = '';
                    //分块处理传入base64串
                    while (mlen > 0) {
                        mysub = mystr.substr(0, 1000);
                        mystr = mystr.substr(1000);
                        mlen = mystr.length;
                        myatb = new Str(mysub);
                        bstr = myatb.getBytes("ISO8859-1");
                        out.write(bstr);
                    }
                    out.close();
                    callback && callback(entry); // 回调
                } catch (ex) {
                    console.log('写入失败');
                }
            } else if (mui.os.ios) {
                var NSData = plus.ios.importClass('NSData');
                var nsData = new NSData();
                nsData = nsData.initWithBase64EncodedStringoptions(mv, 0);
                if (nsData) {
                    nsData.plusCallMethod({
                        writeToFile: fullPath,
                        atomically: true
                    });
                    plus.ios.deleteObject(nsData);
                }
                callback && callback(entry); // 回调
            }
        })
    })
}

0 人点赞