2

I'm working on a simple app that has but a few functions, that is supposed to play audio ether from a web site ie.

function playfile(){

var playing = "http://static.weareswoop.com/audio/charlestown/track_1.mp3";

   var my_media = new Media(playing,        
    function() {
        alert("playAudio():Audio Success");
    },       
    function(err) {
      if(err.message  != undefined) { alert("playAudio():Audio Error: "+err.message); }
      if(err.code  != undefined) { alert("playAudio():Audio Error: "+err.code); }
});

// Play audio
my_media.play();
}

with the html of

<a href="#" onclick="playfile()">play</a>

No matter what I do this does not work on Andriod (version 2.X and 4.X), but does work just fine on iOS (v5 iPad 1 tested).

If I do this against a web based file, I get no response from either the Success or Failure functions, but if I run this agains a local file on the SD car i.e. file:///mnt/sd-card/mining/charlestown/track1.mp3 I get error code 1 without an error message.

Any help would be more than welcomed.

edit: to get the local file version which I am using FileTransfer() download (which works just fine on any OS).

window.appRootDirName = "mining"; // sets the working directory 
var playing =     window.appRootDir.fullPath + "/track1.mp3"; // sets filename

Thus I end up with a URI file:///mnt/sd-card/mining/track1.mp3 which is the exact correct uri when working on 2.x Samsung Galaxy Y.

Again, I must stress, this entire situation works on iOS, but not on Android. On Android when getting a HTTP: based file it gives no error or success, on FILE:// it gives error code 1 when trying to play.

Kara
  • 6,115
  • 16
  • 50
  • 57
Andrew Killen
  • 1,756
  • 24
  • 24

2 Answers2

1

You have to add in the android_asset/www/ to the filepath in order to get it to play from the file see the following bug.

Playing local sound in phonegap

this should solve the playing a local file part of the problem

I tried playing http://static.weareswoop.com/audio/charlestown/track_1.mp3 and it plays fine for me using Cordova 3.0 and android 4.3

Community
  • 1
  • 1
Leo
  • 1,495
  • 23
  • 41
  • Leo, thanks for your answer. I found out after much time it was the system I was forced to use was running a very old version of cordova. This broke most things and the company providing it offered no support or option to upgrade. However I did learn in the meantime while finding that out that your pretty much on the ball, check the urls an paths and things should sort themselves out. – Andrew Killen Oct 31 '14 at 10:12
0

Playing media files does not work when running via the PhoneGap Desktop / PhoneGap Developer apps. It does work when you do a "real" build, for example using PhoneGap Build, and then install the app on your phone.

So as well as using the full file path, as described in other answers, you also need to be running a "real" build.

It looks like this is a bug that PhoneGap know about and have on their list: https://github.com/phonegap/phonegap-app-desktop/issues/335.

Richard Shepherd
  • 1,300
  • 17
  • 20