问题
I'm trying to query playlists in the device from MediaStore. I have followed a question asked before but I didn't get the answer.
This is how I query for playlists
public void addToPlaylist(long playlistId, Context context,
ArrayList<Play> playlistTracks, String playlistName) {
int count = getPlaylistSize(playlistId, context);
Log.d("playlist size=", "" + count);
ContentValues[] values = new ContentValues[playlistTracks.size()];
for (int i = 0; i < playlistTracks.size(); i++) {
values[i] = new ContentValues();
values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i
+ count + 1);
values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID,
playlistTracks.get(i).getId());
values[i].put(MediaStore.Audio.Playlists.NAME, playlistName);
}
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
playlistId);
int num = resolver.bulkInsert(uri, values);
Log.d("songs added=", "" + num); // added
resolver.notifyChange(Uri.parse("content://media"), null);
}
private int getPlaylistSize(long playlistId, Context context)
{
// TODO: Implement this method
return 0;
}
public void retrieveAllPlaylists(Context context) {
Uri tempPlaylistURI = 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 };
Cursor playListCursor = context.getContentResolver().query(
tempPlaylistURI, columns, null, null, null);
if (playListCursor != null) {
Log.d("playlist cursor count=", "" + playListCursor.getCount());
for (boolean hasItem = playListCursor.moveToFirst(); hasItem; hasItem = playListCursor
.moveToNext()) {
playlistName = playListCursor.getString(playListCursor
.getColumnIndex(nameKey));
// noOfTracks = playListCursor.getInt(playListCursor
// .getColumnIndex(tracksCountKey));
Log.d(this.getClass().getName(), "playlistname=" + playlistName // returns only default playliststhe
+ "tracks=" + noOfTracks);
playlistModel.add(new Play(playlistName, noOfTracks));
}
}
}
All I get is a blank screen. Please guide me
回答1:
the simple way to create a playlist is :
public void addnewPlaylist(Context context, String newplaylist) {
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues(1);
values.put(MediaStore.Audio.Playlists.NAME, newplaylist);
resolver.insert(uri, values);
}
then populate the playlist:
public void addTrackToPlaylist(Context context, String audio_id,
long playlist_id, int pos) {
Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlist_id);
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, pos);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);
values.put(MediaStore.Audio.Playlists.Members.PLAYLIST_ID,
playlist_id);
resolver.insert(newuri, values);
}
Looking at your code, you do not build the correct structure. A playlist does not hold the playlist name, simply the Playlist_id.
To return all your playlists :
public Cursor getandroidPlaylistcursor(Context context) {
ContentResolver resolver = context.getContentResolver();
final String id = MediaStore.Audio.Playlists._ID;
final String name = MediaStore.Audio.Playlists.NAME;
final String[] columns = { id, name };
final String criteria = null
return resolver.query(uri, columns, criteria, null,
name + " ASC");
}
回答2:
Example to populate an array :
Cursor cursor = plist.getPlaylistTracks(getActivity(), playlist_id);
// replace with your own method to get cursor
ArrayList<String> audio_ids = new ArrayList<String>();
// build up the array with audio_id's
int i = 0;
if (cursor != null && cursor.moveToFirst()) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
audio_ids.add(audio_id);
}
Make sure that when you assign audio_id (or your own variable) you use the correct MediaStore uri as used when you obtained the cursor. The column names in your cursor determine what you can get here.
来源:https://stackoverflow.com/questions/34883564/querying-playlist-from-mediastore