Cordova Media won't play mp3 from www
android, cordova
Solution
Path of media file incorrect. You must get path correctly:
var getPathMedia = function () {
var path = window.location.pathname;
var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
// Return path at www folder
return 'file://' + phoneGapPath;
};
var media = new Media(getPathMedia() + 'media/message.mp3');
media.play();
Problem
I have found lots of potential resolutions to this issue on StackOverflow but nothing seems to be resolving it for me. I am trying to play an mp3 file within my Cordova (3.5) app using `org.apache.cordova.media@0.2.10` ``` var sound = new Media('audio/el/hello.mp3'); sound.play() ``` But it just won't play and I get the following error in LogCat ``` MediaPlayer error (1, -2147483648) ``` I have tried serving the folder locally and the following works pointing at the same file ``` var sound = new Media('http://10.0.2.2:9999/www/audio/el/hello.mp3'); sound.play() ``` Which suggests that it isn't because the file has an encoding problem. I wasn't able to use the latest version of the `media` plugin because the `deviceready` event never fires. Update: I've just tried unzipping the files to persistent storage and playing them from there and I get the same error. Update: Stepping through the `AudioPlayer.java` source it seems that the `www` directory is not part of the assets because the following call throws a `FileNotFoundException` where `f == "www/el/hello.mp3"` ``` fd = this.handler.cordova.getActivity().getAssets().openFd(f); ``` However If I place the files directly in the `assets` folder then it works.