28

I have a .wav file in my www folder. I am using jQuery with the following code. The alerts go off but the sound does not play. Am I doing something wrong?

<script type="text/javascript" charset="utf-8" src="phonegap-0.9.2.js"></script> 
<script type="text/javascript" charset="utf-8" src="jquery.js"></script> 


<script type="text/javascript" charset="utf-8">

$(document).ready(function () {
    window.alert("READY!");
    document.addEventListener("deviceready", onDeviceReady, true);

    function onDeviceReady(){
        window.alert("OK@!");
        var snd = new Media("test.wav");
        snd.play();
    }
});

</script> 

The sound just does not play.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
james
  • 2,595
  • 9
  • 43
  • 70

3 Answers3

48

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' );
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • 1
    Thank you thank you for this! Phonegap 3.0 was just released and this is STILL undocumented in all the official places. – c0bra Jul 23 '13 at 17:33
  • 2
    This answer put an end to 2 days of misery. Thank you so much. – tomwoods Mar 05 '14 at 00:53
  • 2
    I tried by using the above getPhoneGapPath() function to get the local file audio path in ios. But still its not worked out. Please give me an idea for how to specify the local audio file path in ios. – Sathiyaraj Jul 11 '14 at 16:12
  • This does not work for me either and I'm using Android. I get error code 1. – Curtis Jul 25 '14 at 01:08
  • 1
    Is there any way to get local soundfiles working when using `phonegap serve` instead of a installing the app locally? – Christian Jul 28 '14 at 08:31
  • 1
    @Sathiyaraj The function to get path won't work if called from a file that's not 'index.html' and therefore you must crop more or less than 10 chars from the returned path. 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 Mar 03 '16 at 20:21
  • You need to remove 'file://' in case of IOS – Dhyey Dec 17 '16 at 07:13
32

Try giving an absolute local path. E.g.

new Media("/android_asset/www/test.wav");

That should work on Android. Hopefully they'll fix this in PhoneGap, as it's something of a bug in the cross-device support.

Ben Winters
  • 504
  • 1
  • 6
  • 7
1

I have another version of getPhonegapPath, it detect's when you'r using Android:

function getPhoneGapPath() {
   var path = window.location.pathname;
   path = path.substr( path, path.length - 10 );
   var devicePlatform = device.platform;
   if(devicePlatform=="Android"){
    path = 'file://'+path;
   }
   return path;
};
Marcelo Agimóvel
  • 1,668
  • 2
  • 20
  • 25