Setting data source to an raw ID in MediaPlayer

russoue picture russoue · Aug 16, 2009 · Viewed 36.3k times · Source

In MediaPlayer.create method an id to a Raw file can be used but how to use that in setDataSource method?

Answer

Chris.Zou picture Chris.Zou · Nov 21, 2013

Refer to the source android.media.MediaPlayer

AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null) return;
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();

You may want to add try-catch to the block.