I am writing an android app, and I want to access the playlist created by android default music app.
In my app, the user should be able to browse the playlist and se
This activity combines Ankur Pandya's two answers to list the playlists on the device and then play the first track from the first playlist.
package com.withoutstones.pandyaplaylists;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
/**
* @author Ankur Pandya
* @author Tommy Herbert
*/
public class PandyaPlaylistsActivity extends Activity {
// constants
private static final String LOGGING_TAG = "PandyaPlaylists";
// static methods
/**
* Create a MediaPlayer and play the specified audio file. Note that a full app would usually
* call stop() and release() on the player after use.
*
* @param path
* to data file
*/
public static void playAudio(final String path) {
final MediaPlayer player = new MediaPlayer();
if (path == null) {
Log.e(LOGGING_TAG, "Called playAudio with null data stream.");
return;
}
try {
player.setDataSource(path);
player.prepare();
player.start();
} catch (Exception e) {
Log.e(LOGGING_TAG, "Failed to start MediaPlayer: " + e.getMessage());
return;
}
}
// superclass overrides
@Override
public void onCreate(final Bundle savedInstanceState) {
// Create the superclass portion of the object.
super.onCreate(savedInstanceState);
// Set up the UI.
this.setContentView(R.layout.main);
// Get a cursor over all playlists.
final ContentResolver resolver = this.getContentResolver();
final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String idKey = MediaStore.Audio.Playlists._ID;
final String nameKey = MediaStore.Audio.Playlists.NAME;
final String[] columns = { idKey, nameKey };
final Cursor playLists = resolver.query(uri, columns, null, null, null);
if (playLists == null) {
Log.e(LOGGING_TAG, "Found no playlists.");
return;
}
// Log a list of the playlists.
Log.i(LOGGING_TAG, "Playlists:");
String playListName = null;
for (boolean hasItem = playLists.moveToFirst(); hasItem; hasItem = playLists.moveToNext()) {
playListName = playLists.getString(playLists.getColumnIndex(nameKey));
Log.i(LOGGING_TAG, playListName);
}
// Play the first song from the first playlist.
playLists.moveToFirst();
final long playlistID = playLists.getLong(playLists.getColumnIndex(idKey));
this.playTrackFromPlaylist(playlistID);
// Close the cursor.
if (playLists != null) {
playLists.close();
}
}
// PandyaPlaylistsActivity instance methods
/**
* Play the first track on the specified playlist.
*
* @param playListID
* from the MediaStore database
*/
public void playTrackFromPlaylist(final long playListID) {
final ContentResolver resolver = this.getContentResolver();
final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
final String dataKey = MediaStore.Audio.Media.DATA;
Cursor tracks = resolver.query(uri, new String[] { dataKey }, null, null, null);
if (tracks != null) {
tracks.moveToFirst();
final int dataIndex = tracks.getColumnIndex(dataKey);
final String dataPath = tracks.getString(dataIndex);
PandyaPlaylistsActivity.playAudio(dataPath);
tracks.close();
}
}
}