我有一个函数将mp3文件编码为base64 string。但是当执行文件时会产生一些错误。我的函数如下:
function recordAudioToBase64(){
console.log('encodeing fun reached');
var file = 'jivebirdrecord.mp3';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFile, fail);
}
function gotFile(fileSystem){
fileSystem.root.getFile("jivebirdrecord.mp3", null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry){
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
}
function readDataUrl(file){
var reader = new FileReader();
reader.onloadend = function(evt) {
base = evt.target.result;
};
reader.readAsBinaryString(file);
}
function fail(){
console.log('failed......');
}
我有以下错误:
** processMessage失败:错误:TypeError:无法在'FileReader'上执行'readAsDataURL':参数不是Blob。
processMessage失败:Stack:TypeError:无法在'FileReader'上执行'readAsDataURL':参数不是Blob。
at FileReader.readAsDataURL (file:///android_asset/www/plugins/org.apache.cordova.file/www/FileReader.js:201:33)
at readDataUrl (file:///android_asset/www/js/script.js:293:14)
at gotFile (file:///android_asset/www/js/script.js:283:4)
at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:52:25
at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
at Object.cordova.callbackFromNative (file:///android_asset/www/cordova.js:292:54)
at processMessage (file:///android_asset/www/cordova.js:1039:21)
at Function.androidExec.processMessages (file:///android_asset/www/cordova.js:1076:13)
at pollOnce (file:///android_asset/www/cordova.js:944:17)
at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:939:5)
processMessage failed: Message: S01 File810616738
[{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/storage\/emulated\/0\/Android\/data\/com.phonegap.helloworld\/cache\/","filesystem":0,"isFile":false,"name":""},
{ “FULLPATH”: “/”, “filesystemName”: “持续性”, “isDirectory”:真 “nativeURL”: “文件:///存储/模拟/ 0 /”, “文件系统”:1, “ISFILE”:假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “内容”, “isDirectory”:真 “nativeURL”: “cdvfile://本地主机/内容/”, “文件系统”:1, “ISFILE” :假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “文件”, “isDirectory”:真 “nativeURL”: “文件:///data/data/com.phonegap.helloworld/files/”, “文件系统”:1, “ISFILE”:假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “文件外部的”, “isDirectory”:真 “nativeURL”:“文件:///存储/模拟/ 0 / Android设备/数据/ COM。 phonegap.helloworld /文件/”, “文件系统”:1, “ISFILE”:假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “文件”, “isDirectory”:真 “nativeURL”:“文件:///data/data/com.phonegap.helloworld/files/Documents/ “ ”文件系统“:1, ”ISFILE“:假, ”姓名“: ”“},{ ”FULLPATH“: ”/“, ”filesystemName“: ”SD卡“, ”isDirectory“:真 ”nativeURL“:”文件:///存储/模拟/ 0 /”, “文件系统”:1, “ISFILE”:假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “高速缓存”, “isDirectory”:真 “nativeURL”: “文件:///data/data/com.phonegap.helloworld/cache/”, “文件系统”:1, “ISFILE”:假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “高速缓存外部”, “isDirectory”:真 “nativeURL”:“文件:///存储/模拟/ 0 / Android设备/数据/ COM。 phonegap.helloworld /高速缓存/”, “文件系统”:1, “ISFILE”:假, “姓名”: “”},
{ “FULLPATH”: “/”, “filesystemName”: “根”, “isDirectory”:真 “nativeURL”: “文件:///”, “文件系统”:1, “ISFILE”:假, “名称”: “”}] **
请帮我找一个使用phonegap和jquery mobile将mp3文件转换为base64字符串的解决方案。 Thnaks
答案 0 :(得分:0)
有点晚了但对其他人来说这可能会有所帮助。
directoryReader.readEntries(function(entries){
for (var i=0; i<entries.length; i++) {
var reader = new FileReader();
var entry = entries[i];
entry.file(function(file){
reader.readAsDataURL(file);
reader.onload = doOnload(entry.name);
});
}
});