How to get Path for local mp3 file from www in Pho

2020-03-01 20:22发布

问题:

i am using phonegap audio plugin in my phonegap app. I have some audio files stored in www/audio folder.

For android i used "file:///android_asset/www/audio" to read local file.

For IOS i need help.

Thanks

回答1:

Finally got it, reference:link

To get path i used this function

function getPhoneGapPath() {
   var path = window.location.pathname;
   path = path.substr( path, path.length - 10 );
   return path;
};

USAGE:

For IOS:

var snd = new Media( getPhoneGapPath() + 'test.mp3' );

For Android:prepend 'file://'

var snd = new Media( 'file://' + getPhoneGapPath() + 'test.mp3' );



回答2:

You will have trouble if called from a file other than 'index.html' because the part to trim off might not be 10 chars.

Try this:

// Get absolute file path on the device.
function getPhoneGapPath() {
    var path = window.location.pathname;
    var sizefilename = path.length - (path.lastIndexOf("/")+1);
    path = path.substr( path, path.length - sizefilename );
    return path;
};