Search code examples
cordovacordova-pluginsvisual-studio-cordova

Cordova 3+ find all music files on device


I am trying to learn Cordova, using visual studio as my ide. I've followed some instructions online to build a music player, but one plugin is missing... "Storage".

I tried running it anyway but I get 'error 5' when trying to use directoryReader.

I'm assuming this is due to a permission issue which again I assume would be sorted by the storage plugin.

A couple of questions here...

Are my above assumptions correct? Where can I find the Storage Plugin? How would I include the Plugin?

Thanks in advance for any advice.

(I know I should post code but I'm not home now and this is driving me mad thinking about it!)

Here's a sample of code I'm using:

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

Solution

  • On the version 3.0 of the file plugin, it changed the LocalFileSystem.PERSISTENT path to be a path owned by the app, so it will be deleted on app uninstall and can't be accessed by other apps (as recommended by android best practises on storage)

    If you want to keep using the old path, you have to add this line on your config.xml

    <preference name="AndroidPersistentFileLocation" value="Compatibility" />