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

瘦欲@ 提交于 2019-12-05 07:46:03

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

I finally managed to get it working!!

I first initialising the audio file by finding the full Android path using @Sarim Sidd help above 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 to play the audio I forced the volume to full (1.0) before calling the play method. For some reason I noticed the audio kept reseting to mute each time I started the app:

//PLAY BELL SOUND THROUGH APP
sound.setVolume(1.0);
sound.play();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!