Phonegap app playing mp3 using Media plugin does n

2019-06-07 22:29发布

I've developed file reader in phonegap with following path:

var defaultPath = 'file:///storage/sdcard0/';

So i'm able to list all file on sdcard and select one folder with audio files to load. So my list of audio files looks like:

i[1] = 'file:///storage/sdcard0/Download/audio/file.mp3';
i[2] = 'file:///storage/sdcard0/Download/audio/file1.mp3';

Then I try to init song using Media plugin ( installed ) with following piece of code:

var media = new Media(i[1]);
media.play();

But audio does not play or do nothing. But when I'm trying to test it using PhoneGap APP all works fine this just isn't work when I make build and try to test app from build.

1条回答
太酷不给撩
2楼-- · 2019-06-07 23:08

I found the issue by myself. Android does not let read files on local storage or on sdcard via path starting from root /storage/sdcard0 /storage/sdcard1. So when we would like to access local storage or sdcard we must access it via file:///mnt/sdcard/ for sdcard and file:///mnt/storage/sdcard1/ for phone storage.

Small example how to read entries from sdcard or phone storage:

var path = 'file:///mnt/storage/sdcard1/'; 
window.resolveLocalFileSystemURL(path, function(entry){
    var reader = entry.createReader();
    reader.readEntries(showEntries);
});

function showEntries(entries){
   var entries = [];
   for(var i =0; i < entries.length; i++){
       var entry = entries[i];
       var data = {
           name: entry.name,
           path: entry.fullPath,
       };

       entries[i] = data;
   }

   console.log(JSON.stringify(entries));
}

Hope that this will help with same issue as I had because this is not documented in phonegap.

查看更多
登录 后发表回答