ghi
ghi

Reputation: 725

Phonegap - sounds not working

I've spended several hours trying to make my phonegap app work whith mp3 files. I've already tested two cordova versions(1.9 and 2.0). I've tried with files bigger and smaller than 1MB. Nothing helped. Sound work fine with files frome internet:

my_media = new Media("http://www.example.com/test.mp3");
my_media.play();   

But it cannot work whith local files:

my_media = new Media("/android_asset/www/sounds/test.mp3");
my_media.play();   

LogCat in Eclipse throws error:

09-13 18:55:58.085: W/System.err(7525): java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
09-13 18:55:58.085: W/System.err(7525):     at android.content.res.AssetManager.openAssetFd(Native Method)
09-13 18:55:58.101: W/System.err(7525):     at android.content.res.AssetManager.openFd(AssetManager.java:330)
09-13 18:55:58.101: W/System.err(7525):     at org.apache.cordova.AudioPlayer.startPlaying(AudioPlayer.java:223)
09-13 18:55:58.101: W/System.err(7525):     at org.apache.cordova.AudioHandler.startPlayingAudio(AudioHandler.java:227)
09-13 18:55:58.125: W/System.err(7525):     at org.apache.cordova.AudioHandler.execute(AudioHandler.java:75)
09-13 18:55:58.125: W/System.err(7525):     at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:185)
09-13 18:55:58.125: W/System.err(7525):     at java.lang.Thread.run(Thread.java:1019)

Is anyone has any suggestions what may I doing wrong?

Upvotes: 0

Views: 468

Answers (1)

Duke
Duke

Reputation: 1731

one of the android basic concept is that your asset files compressed and you cant access them directly. use :

getAssets().open(mp3FileUrl)

instead to get InputStream of mp3 file

UPDATE : because you are using phonegap best thing i can suggest is first unpack files you need to your data directory after that you can use them directly . for unpacking use :

window.resolveLocalFileSystemURI("file:///android_asset/www/foo.html", onResolveSuccess, onFail);

and them copy it with phonegap file API. you can access your file in /data/data/packagename/filename

Upvotes: 1

Related Questions