Search code examples
androidaudiomedia-playeruri

android MediaPlayer res raw folder getContentUri


I found this tutorial and am trying to implement it in my project http://www.androiddevblog.net/android/playing-audio-in-android

I want to read music from res/raw folder instead of external storage

Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

Here is my code:

Cursor cursor = getContentResolver().query( MediaStore.Audio.Media.getContentUri("android.resource://com.varma.samples.musicplayer/raw/"), null, null, null, null);

Of course the Uri does not return anything and I want to get the audio from res/raw...

Thank you!!


EDIT:

Thank you both, I won't use a cursor then ;)

If it helps somebody I am using this for get all songs:

Field[] fields = R.raw.class.getFields();

Solution

  • There is no need to use a Cursor here.

    Please search in the official developer docs before posting your question here. Specifically the Media Playback page contains an example for how to do what you are trying.

    MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
    mediaPlayer.start(); // no need to call prepare(); create() does that for you