3

I have my phonegap 3.5 app with angularjs and I have my song.mp3 in my root directory with the index.html file The app works great in my android device and in phonegap application but I can't play the song.mp3 file I use the cordova media plugin and it always gives me the code 1 error I tried also the ngCordova plugin the same code error . my code is

 var src ="song.mp3";
 // I tried also 
//  var src="/android_asset/www/song.mp3"; 
 // and src ="file:///song.mp3";

var media = new Media(src,function(){

   console.log("playing");
 ,function(err){

 console.log(err.code);
 });
media.play();
Remon Amin
  • 1,506
  • 2
  • 19
  • 44
  • Hi, i am facing same problem.How did you get path of local file in IOS? http://stackoverflow.com/questions/28339389/how-to-get-path-for-local-mp3-file-from-www-in-phonegap-ios?noredirect=1#comment45024148_28339389 – Varun Nayyar Feb 05 '15 at 09:01

2 Answers2

2

Code = 1 means "aborted" (MediaError.MEDIA_ERR_ABORTED = 1) see https://github.com/apache/cordova-plugin-media/blob/master/doc/index.md#constants-1

So this usually means that there is something wrong with the path. Is song.mp3 in the same folder as your index.html? Maybe this helps: Playing local sound in phonegap

You will need file://... only if you wanna play from the local filesystem.

Community
  • 1
  • 1
fr00t
  • 681
  • 1
  • 4
  • 19
1

Its working for me, I have used cordova media plugin with the following scrip

cordova plugin add org.apache.cordova.media

function getCordovaPath() {
  var absolutePath = window.location.pathname;
  //14 is length of html file name(including .html) 
  actualPath = absolutePath.substr( path, path.length - 14 );
  return 'file://' + actualPath;
}

function playAudio() {
  //var audioElement = document.getElementById(id);
  //var url = audioElement.getAttribute('src');
  var my_media = new Media(getCordovaPath() + 'beep.mp3',
  // success callback
  function () { console.log("playAudio():Audio Success"); },
  // error callback
  function (err) { console.log("playAudio():Audio Error: " + err); });
  // Play audio
  my_media.play();
}
<body onload="playAudio()">
Ganesh
  • 11
  • 1