Sound will not play on Android via phone hang, but works fine on iOS

It works great on iOS.

I looked through many answers, including these 2:

Play sound in Phonegap for Android

HTML5 sound does not play in PhoneGap (can I use Media?)

I tried all the solutions:

  • Changing the path to /android_asset/www/ for Android
  • Using .ogg Files
  • Preload the sound using 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(); 

Does anyone have any ideas? I seem to be spinning in circles.

+5
source share
3 answers

I finally managed to get it to work!

First, I initialized the sound file, finding the full path to Android, using the @Sarim Sidd help provided, and then preloading the audio file:

 //INITIALISE SOUND if(device.platform === "Android") //DIFFERENT PATH FOR ANDROID { //navigator.notification.beep(1); var path = window.location.pathname; path = path.substr( path, path.length - 10 ); //strip off index.html url = 'file://'+path+"sound.ogg"; }else{ url = "sound.mp3"; } //sound = new Media(url); sound = new Media(url); sound.play(); sound.pause(); 

Then, when I was playing back the sound, I forcibly increased the volume (1.0) before calling the playback method. For some reason, I noticed that the sound saved in reseller mode is muted each time the application starts:

 //PLAY BELL SOUND THROUGH APP sound.setVolume(1.0); sound.play(); 
0
source

I had a similar one and this solution worked for me. Get the full path to your application using window.location.pathname , so you don't have to worry about the type of device, it will provide you with the full path, including index.html , and using the below function, just disconnect index.html from the line and add 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

+2
source

This gave me a ton of problems for Android, but I finally solved it. You can find the solution described here: Solution

+1
source

All Articles