Cordova 3+ 查找设备上的所有音乐文件

Cordova 3+ find all music files on device

我正在尝试学习 Cordova,使用 visual studio 作为我的 ide。我已经按照一些在线说明构建了一个音乐播放器,但是缺少一个插件... "Storage".

无论如何我都试过 运行 但我在尝试使用 directoryReader 时得到 'error 5'。

我假设这是由于权限问题导致的,我再次假设会由存储插件进行排序。

这里有几个问题...

我的上述假设是否正确? 我在哪里可以找到存储插件? 我将如何包含插件?

提前感谢您的任何建议。

(我知道我应该 post 编码,但我现在不在家,这让我发疯!)

这是我正在使用的代码示例:

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );
function onDeviceReady() {
    // Handle the Cordova pause and resume events
    document.addEventListener( 'pause', onPause.bind( this ), false );
    document.addEventListener( 'resume', onResume.bind( this ), false );
    $('#readMusic').click(function () { readMusic(); });
};
function readMusic() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
    var directoryReader = fileSystem.root.createReader();
    directoryReader.readEntries(readerSuccess, fail);
}
function readerSuccess(entries) {
    var i;
    for (i = 0; i < entries.length; i++) {
        if (entries[i].name.indexOf('.mp3') > -1) {
            alert(entries[i].name);
        }
    }
}

Cordova 不提供官方存储插件。您可以使用 localStorage 按以下方式存储值:

保存:localStorage.setItem("key",value);

并用于检索:localStorage.getItem("key");

但是,您的应用程序似乎失败了,因为它无法从您设备的文件系统中读取信息。试试这个插件:https://github.com/apache/cordova-plugin-file

此插件实现了一个文件 API,允许 read/write 访问驻留在设备上的文件。

让我知道它是否有效。

文件插件3.0版本,将LocalFileSystem.PERSISTENT路径修改为应用拥有的路径,卸载应用时会被删除,其他应用无法访问(根据 android 存储最佳实践的建议)

如果您想继续使用旧路径,则必须在 config.xml

中添加此行
<preference name="AndroidPersistentFileLocation" value="Compatibility" />