Setting data source to an raw ID in MediaPlayer

Viewed 40209

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

3 Answers

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.

Related