Phonegap 3.5 plays local audio files

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

 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();

      

+3


source to share


2 answers


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 something is wrong along the way. Song .mp3 in the same folder as index.html? Maybe this helps: Play local sound in phone screensaver



You file://...

only need it if you want to play from your local filesystem.

+2


source


It works for me, I used media cordia plugin with the following script

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()">
      

Run codeHide result


+1


source







All Articles