Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio will not play on Android using phonegap, but works fine on iOS

It works fine on iOS.

I have looked many answers including these 2:

Play sound on Phonegap app for Android

HTML5 audio not playing in PhoneGap App (Possible to use Media?)

I have tried all of the solutions:

  • Changing the path to /android_asset/www/ for Android
  • Using .ogg files
  • Pre load the audio with a play() / pause().
  • Using the Media plugin provided by Cordova/Phonegap

Here is my current code:

if(device.platform === "Android")   //DIFFERENT PATH FOR ANDROID
{
    url = "/android_asset/www/sound.ogg";
}else{
    url = "sound.mp3";
}
alert(url);
sound = new Media(url);
sound.play();

Anyone have an ideas?? It feels like I have been going round in circles

like image 346
smj2393 Avatar asked May 15 '15 12:05

smj2393


1 Answers

I had similar and this solution worked for me Get complete path of your application using window.location.pathname, this way you dont need to worry about device type, it will give you the complete path including index.html and by using the below function simply strip off index.html from the string and append your media file.

function getPhoneGapPath() {

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

};

And then

url = getPhoneGapPath()+'sound.ogg';  //OR MP3 

alert(url);
sound = new Media(url);
sound.play();

Hope this helps

like image 131
Sarim Sidd Avatar answered Oct 10 '22 21:10

Sarim Sidd