Kenny Wyland
Kenny Wyland

Reputation: 21870

FileNotFoundException with MediaPlayer.create() reading from /res/raw

I have a tiny uncompress .wav file in my /res/raw directory called keyclick.wav (/res/raw/keyclick.wav).

However, I'm occasionally seeing an exception thrown which causes the activity to crash:

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed at ...
android.content.res.AssetManager.openNonAssetFdNative(Native Method)
android.content.res.AssetManager.openNonAssetFd(AssetManager.java:427)
android.content.res.Resources.openRawResourceFd(Resources.java:857)
android.media.MediaPlayer.create(MediaPlayer.java:662)

It says "it is probably compressed" but I've double checked, it is not compressed.

This is how I instantiate the MediaPlayer:

this.clickPlayer = MediaPlayer.create(this.getActivity(), R.raw.keyclick);

I uploaded the file so you can see it directly:

http://inadaydevelopment.com/stackoverflow/keyclick.wav

The file is only 664 bytes and it's not compressed. Why is the system failing to get a file descriptor?

Upvotes: 0

Views: 817

Answers (1)

William Seemann
William Seemann

Reputation: 3530

Kenny, have you tried playing the file using this approach:

MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.keyclick);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();

Upvotes: 1

Related Questions