I believe what you're looking for is exactly the MediaStorage
Android API.
It has so many ways to access any type of media that is in the device's internal and external storage.
To get all the songs on the device, check out the MediaStorage.Audio
and to get playlists recorded on the device at a glance at MediaStorage.Audio.PlayLists
.
I found a complete tutorial to build a Music Player app using this API, at link .
You'll have to use a ContentResolver to make a Query
about songs and PlayLists. It would have something like:
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
// Iterar sobre o cursor...
Below is the code for playing a song using MediaPlayer
from Cursor
, from this SO question:
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
int music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musicCursor.moveToPosition(position);
String filename = musicCursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
}
});
} catch (Exception e) {}
}
In this code, a query is made for all songs on the device.
To check the playlists, I found a snippet
, without a reliable source that follows the same principle of music query:
public void checkforplaylists(Context context) {
ContentResolver cr = context.getContentResolver();
final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String id = MediaStore.Audio.Playlists._ID;
final String name = MediaStore.Audio.Playlists.NAME;
final String[] columns = {id, name};
final Cursor playlists = cr.query(uri, columns, null, null, null);
if(playlists == null) {
Log.e(TAG,"Found no playlists.");
return;
}
Log.e(TAG,"Found playlists.");
// Iterar sobre o cursor...
}