Reputation: 16978
I've been stuck on this particular problem for a while now, and it's something that should be really simple so it's annoying.. I must be misusing the Android API somehow?
Here is the code:
try {
String packageName = getPackageName();
int resID = getResources().getIdentifier( "filename" , "raw" , packageName ); //in res/raw folder I have filename.wav audio file
mp.setDataSource("android.resource://" + packageName + "/" + resID);
mp.prepare();
mp.start();
} catch (Exception e) {
Toast.makeText(PlayScreen.this, e.toString(), Toast.LENGTH_LONG).show();
}
The toast message I see says:
java.io.IOException: Prepare failed.: status=0xFFFFFFFC
In eclipse logcat I see:
MediaPlayer Error (-4 -4)
Much thanks to anyone who can help with this... I just want to play my wav files darn it!
Upvotes: 0
Views: 3353
Reputation: 134664
Try using a FileDescriptor instead:
String packageName = getPackageName();
int resID = getResources().getIdentifier( "filename" , "raw" , packageName );
AssetFileDescriptor afd = getResources().openRawResourceFd(resId);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
afd.close();
That's what I always do and it's safer to do it that way rather than hardcoding a path.
Upvotes: 1