4

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

Varun Nayyar
  • 887
  • 4
  • 20
  • 46
  • may be the [link1](http://stackoverflow.com/questions/10945375/phonegap-ios-how-to-get-app-documents-folder-full-path) support you – Anbu.Karthik Feb 05 '15 at 08:39
  • Does this answer your question? [Playing local sound in phonegap](https://stackoverflow.com/questions/4438822/playing-local-sound-in-phonegap) – Vega Sep 24 '20 at 03:39

2 Answers2

6

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

Community
  • 1
  • 1
Varun Nayyar
  • 887
  • 4
  • 20
  • 46
4

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;
};
Sherri
  • 816
  • 2
  • 9
  • 18
  • This is great for dealing with unexpected path sizes. This is a better method to get the path than subtracting a fixed length of 10. – Devin Carpenter Apr 02 '17 at 22:33
  • In case the app name contains unicode characters which are encoded into %NN in the window-location-pathname, you will have to turn it back into unicode using `path = decodeURIComponent(path)` – ishahak Jul 03 '17 at 04:33