Playing local sound in phonegap

You can use window.location.pathname to get the path of your application in any PhoneGap app. That way you don’t have to hardcode it for Android. It will look something like this on iPhone:

/var/mobile/Applications/{GUID}/{appname}.app/www/index.html

And this on Android:

/android_asset/www/index.html

Strip off the /index.html, prepend file://, and append your file test.wav.

Demo: http://jsfiddle.net/ThinkingStiff/r7eay/

Code:

function getPhoneGapPath() {

    var path = window.location.pathname;
    path = path.substr( path, path.length - 10 );
    return 'file://' + path;

};

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

Leave a Comment