You must first consider whether you really want to play your audio files directly through the file system. A more common way to work with Android is to access media files by querying the built-in multimedia database instead of working directly with the file system. If you want to work with a multimedia database, you usually do this:
- Acquiring an instance of a content recognizer (this is usually done by calling
getContentResolver() in your activity) - You can then request a ContentResolver for the available media of a particular type. This is where you get in contact with the URI for the first time. You tell ContentResolver which media you are viewing by specifying a specific URI for your request. Since you are interested in music, you would request android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.
Now you can request your ContentResolver as follows:
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Cursor cursor = contentResolver.query(uri, null, null, null, null);
This is the most basic query that gives you all registered multimedia objects. By specifying other (NULL) parameters, you can specify filter criteria, sorting criteria, etc.
The request contains an instance of the cursor. With a cursor instance, you can basically do two things.
- You can iterate the results of your query.
- You can ask a question about the structure of the columns of your results (for example, the column index of the name TITLE, artist name ARTIST, and the last but not least significant identifier column).
So, let's assume that you are only interested in the identifier of the first song. You do this by setting your Cursor to its beginning, calling:
cursor.moveToFirst();
Now you can simply type the printed cursor getters to access the columns you are interested in, for example, for the identifier of this song, which you would call:
long mySongId=cursor.getLong(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID));
If you want to know the name of the album you would do:
cursor.getString(android.provider.MediaStore.Audio.Media.ALBUM);
Obviously, I skipped all error handling for brevity (in the real world example, you should check if there are any songs on your device, that the request has worked, ...).
Now that you have your song id in mySongId, you can go for the URI thing a second time. This time you can create a full URI for your song by calling:
Uri mySongUri=ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mySongsId);
This gives you the URI of your media file, which you can then give the media player to start playing your file:
myMPlayer = new MediaPlayer(); myMPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); myMPlayer.setDataSource(getApplicationContext(), mySongUri);
Be sure that a good song will be the first song on your device so that you can listen to good music after all the hard work :-)
btw: If you want to learn more about the Android concepts of ContentProvider and ContentResolver, you can check out this tutorial: http://www.grokkingandroid.com/android-tutorial-content-provider-basics/