accessing media items in phonegap in www

113 Views Asked by At

I have a mp3 file that i want to be played based on user actions.(a dom event) i have it in www/audio/a.mp3 I am using cordova-plugin-media to handle running the media, but the problem is i cant figure out the file path correctly;

i am using the code below to create the media object

let media = new Media(cordova.file.applicationDirectory+"www/audio/a.mp3", onSuccess, onError);

but the file doesn't play on my device. what should i do? help me if you can.

2

There are 2 best solutions below

2
Eric On

You don't weed to use cordova to access your www directory. Simply try

new Audio('/audio/a.mp3');
0
MStoner On
function loadAudio(file) {
   var path = window.location.pathname;
   path = path.substr(path, path.length - 10);
   var id = path + file;
   console.log('loading audio: ' + id);
   var my_media = new Media(id,
    function () { console.log("loadAudio():Audio Success on id " + id); },
    function (err) { console.log("loadAudio():Audio Error: " + JSON.stringify(err) + ' on id ' + id); }
   );
   return my_media;
};

audioNewWord = loadAudio('audio/newWord.wav');

audioNewWord.play();