简体   繁体   中英

phonegap 3.5 play local audio files

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM