Android MP3: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
I am trying to play a custom sound res/raw/notification.mp3
. It is ~91KB.
Here is my code
MediaPlayer.create(context, R.raw.notification).start();
Here is my error
04-14 15:57:55.387: ERROR/AndroidRuntime(233): android.content.res.Resources$NotFoundException: File res/raw/notification.mp3 from drawable resource ID #0x7f040000
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.content.res.Resources.openRawResourceFd(Resources.java:860)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.media.MediaPlayer.create(MediaPlayer.java:641)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at mypackage.MyActivity$1.onPostExecute(MyActivity.java:123)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.os.AsyncTask.finish(AsyncTask.java:417)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.os.Looper.loop(Looper.java:123)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at java.lang.reflect.Method.invoke(Method.java:521)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-14 15:57:55.387: ERROR/AndroidR开发者_Python百科untime(233): at dalvik.system.NativeStart.main(Native Method)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:426)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): at android.content.res.Resources.openRawResourceFd(Resources.java:857)
04-14 15:57:55.387: ERROR/AndroidRuntime(233): ... 14 more
Same thing happens to an unrelated WAV file so it is probably not the MP3's problem.
What might be wrong? Should I uncompress the WAV/MP3? How?
It turns out to be a problem with the NetBeans generated build scripts. I did a tutorial from scratch and it failed in NetBeans and worked in Eclipse. I suppose I will finally learn Eclipse. It is something I have been wanting to do at least as a comparison in hopes it would be less buggy, but I have not had the time. Well necessity is going to gave me the time now.
What are you passing for the context value? Context is used to know where to look for the resource file. It looks like you may have called this inside an AsyncTask? Are you using getApplicationContext()
to get a reference? For instance, if this code were inside an Activity, you would call this method as:
MediaPlayer.create(this, R.raw.notification).start();
If not, you need to pass a proper context that maps to your application package.
Another possibility is your R.java is out of date. Make sure your XML files don't have any errors keeping aapt from rebuilding the resources, and maybe run a Clean on the project.
Another thing you could try is to place your MP3 file into the assets directory and try to load the sound this way. For a file located at assets/notification.mp3
:
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor afd = getAssets().openFd("notification.mp3");
mp.setDataSource(afd.getFileDescriptor());
afd.close();
mp.prepare();
mp.start();
Try this in an Activity first, as getAssets()
needs to be called from a Context. This method circumvents issues that may be occuring in the resources bundle and may better determine where the true culprit is.
Hope that helps!
精彩评论