我在应用程序中使用需要作为缓冲区数组获取的音频文件。为此,我让用户选择一个文件(using Ionic/Cordova FileChooser Plugin),然后获得一个URL,如:
content://com.android.providers.media.documents/document/audio%3A8431
此后,我将其发送到Cordova Plugin File resolveNativePath
函数,并得到如下路径:
file:///storage/emulated/0/Prueba interno/Interno, Teddybär, Dreh Dich Um__320kbps.mp3
在这里,我做我的audioFileInfo
对象
audioFileInfo = {name: "Interno, Teddybär, Dreh Dich Um__320kbps.mp3",
originalPath: "content://com.android.providers.media.documents/document/audio%3A8431",
path: "file:///storage/emulated/0/Prueba interno"}
最后我调用filePlugin.readAsArrayBuffer(audioFileInfo.path, audioFileInfo.name)
来获取缓冲区数组。
当文件位于设备内部存储中时,它可以正常工作,但是当文件来自SD卡时,它就无法工作,因为readAsArrayBuffer
返回“找不到”。
SD卡:
文件选择器URL
content://com.android.externalstorage.documents/document/3D91-1C14%3AM%C3%BAsica%20Dana%2F1%20-%20Teddyb%C3%A4r%2C%20Teddyb%C3%A4r%2C%20Dreh%20Dich%20Um__320kbps.mp3
resolveNativePath:
file:///sdcard/Música Dana/1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3
audioFileInfo:
audioFileInfo = {
name :"1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3"
originalPath : "content://com.android.externalstorage.documents/document/3D91-1C14%3AM%C3%BAsica%20Dana%2F1%20-%20Teddyb%C3%A4r%2C%20Teddyb%C3%A4r%2C%20Dreh%20Dich%20Um__320kbps.mp3",
path : "file:///sdcard/Música Dana"
}
readAsArrayBuffer:
FileError {code: 1, message: "NOT_FOUND_ERR"}
我尝试了FilePlugins的resolveLocalFilesystemUrl()
,并得到了这个Entry
对象:
{
filesystem: {
name: "content",
root: {
filesystem {
name: "content",
root: "...."
}
},
fullPath: "/",
isDirectory: true,
isFile: false,
name: "",
nativeURL: "content://",
},
fullPath: "/com.android.externalstorage.documents/document/3D91-1C14:Música Dana/1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3",
isDirectory: false,
isFile: true,
name: "1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3",
nativeURL: "content://com.android.externalstorage.documents/document/3D91-1C14%3AM%C3%BAsica%20Dana%2F1%20-%20Teddyb%C3%A4r%2C%20Teddyb%C3%A4r%2C%20Dreh%20Dich%20Um__320kbps.mp3",
}
因为我不知道在path
函数的第一个参数中用什么作为readAsArrayBuffer
。
如果我使用fullPath
和name
,则会引发编码错误。
如果我只是从fullPath
中获得不带名称的“路径”,它还会引发编码错误。
有人有类似的经历吗?
答案 0 :(得分:0)
我最终用resolveLocalFilesystemUrl
返回的信息制作了自己的FileReader来处理文件,请注意,此代码使用Typescript
let entry = await this.filePlugin.resolveLocalFilesystemUrl(audioFileInfo.originalPath);
let fileEntry: FileEntry = entry as FileEntry;
console.log("fileEntry", fileEntry);
let readFilePromise = new Promise < any > (resolve => {
console.log("getting File from fileEntry");
// fileEntry.getMetadata(data => {
// console.error("metadata", data);
// });
fileEntry.file((file) => {
console.log("File", file);
var reader = new FileReader();
reader.onloadend = () => {
console.log("Successful file read: ", reader.result);
resolve(reader.result);
};
reader.onprogress = (progressEvent) => {
let fileUpdateInfo: FileLoadingUpdateInfo = {
audioFileInfo: audioFileInfo,
total: progressEvent.total,
loaded: progressEvent.loaded,
percent: parseFloat((progressEvent.loaded * 100 / progressEvent.total).toFixed(2))
}
this.updateAudioFileLoadingText$.next(fileUpdateInfo);
};
reader.onerror = (e) => {
console.error("The reader had a problem", e)
resolve(undefined);
}
reader.onabort = (e) => {
console.error("We aborted...why???", e)
resolve(undefined);
}
console.log("using reader to readAsArrayBuffer ");
reader.readAsArrayBuffer(file);
}, (e) => {
console.error(e)
});
});
let fileBufferArray = await readFilePromise;
console.log("fileBufferArray", fileBufferArray);
return fileBufferArray;