Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Media won't play mp3 from www

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 [email protected]

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.

like image 882
Dave Taylor Avatar asked Jul 08 '14 08:07

Dave Taylor


1 Answers

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();
like image 70
Hanh Le Avatar answered Oct 05 '22 11:10

Hanh Le