First you should think about whether or not you really want to play your audio files directly via the filesystem. The more common way to work on android is to access media files by querying the inbuilt media database instead of working directly with the file system.
If you want to work with the media database you typically do so by:
- Acquiring an instance of a content resolver (usually done by calling
getContentResolver()
in your activity)
- You can then ask the ContentResolver for the available media of a certain type. Here you come in to contact with an URI for the first time. You tell the ContentResolver which kind of media you are intersted in by specifying a specific URI for your query. Since you are interested in Music you would query android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.
You can now query your ContentResolver like this:
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 which gives you all the registered media objects. By specifying the other (NULL) parameters you could specify filter criteria, sort criteria,etc..
Your query gives you an instance of Cursor. With your cursor instance you can basically do two things.
- You can iterate through the results of your query
- You can ask it about the column structure of your results (e.g. the index of the name column TITLE, the Artist name ARTIST and last but not least the ID column).
So lets assume you are interested only in the ID of the first song. You do this by setting your Cursor to the its beginning by calling:
cursor.moveToFirst();
You can now simply call the typed getters of cursor to access the columns you are interested in, so for example for the ID of this song you would call:
long mySongId=cursor.getLong(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID));
If you want to know the album name you would do:
cursor.getString(android.provider.MediaStore.Audio.Media.ALBUM);
Obviously I omitted all the error handling for brevity (in a real world example you should check whether there are any songs on your device, that the query succeeded,...).
Now since you have your songs ID in mySongId you can go for the URI thing a second time. This time you can generate a fully qualified 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 that you can then give to the mediaplayer to start playing back your file:
myMPlayer = new MediaPlayer();
myMPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
myMPlayer.setDataSource(getApplicationContext(), mySongUri);
Be sure to have a good song as the first song on your device so you get to listen to some good music after all the hard work:-)
btw.: If you want to know more about Android's ContentProvider and ContentResolver concepts you can check out this tutorial:
http://www.grokkingandroid.com/android-tutorial-content-provider-basics/