Android Create Playlist
Anyone know how to add playlists to Android in code?
I kind of get that I have to insert it into the content resolver but do I have to just put the song id in or do I have to put all of the song's data in?
I have been looking for sample code but haven't found any yet.
EDIT: Found an answer here is how I do it:
public static void addToPlaylist(ContentResolver resolver, int audioId) {
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + audioId));
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
resolver.insert(uri, values);
}
public static void removeFromPlaylist(ContentResolver resolver, int audioId) {
开发者_运维技巧 Log.v("made it to add",""+audioId);
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
resolver.delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID +" = "+audioId, null);
}
To get the question out of "not answered" I have pasted the OP's code here:
public static void addToPlaylist(ContentResolver resolver, int audioId) {
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + audioId));
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
resolver.insert(uri, values);
}
public static void removeFromPlaylist(ContentResolver resolver, int audioId) {
Log.v("made it to add",""+audioId);
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
final int base = cur.getInt(0);
cur.close();
ContentValues values = new ContentValues();
resolver.delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID +" = "+audioId, null);
}
To answer Jaroslav Záruba comment, the code is better with the PLAY_ORDER of added track set as follows:
cur.moveToLast();
final int base = cur.getInt(cur.getColumnIndex(Playlists.Members.PLAY_ORDER));
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER,
Integer.valueOf(base + 1));
Two major things change : We use the last element of the playlist (cur.moveToLast()) and we add 1 to its PLAY_ORDER value to determine the new track's PLAY_ORDER. The point is to have successive tracks in the playlist.
You can also add 10 for example so that you can insert tracks before or after your new track. I also changed the way we get the id of track. Indeed we don't want to have any problem getting wrong data so we specify the column we want.
This is corrected code which remove song from Playlist:
public static void removeFromPlaylist(ContentResolver resolver, int audioId)
{
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
resolver.delete(uri, MediaStore.Audio.Playlists.Members._ID +" = "+audioId, null);
}
Use this the code itself is self explanatory. It will add song with given id = songID
to playlist with name playlistName
If playlist already exist it will add to existing one or it will create new and then add song to it
/**
* This function add song with id songID to playlist playlistName
* if playlist does exist it will add to exiixting one or it will create new
*
* @param playlistName
* @param songID
*/
private void addToPlaylist(String playlistName, int songID) {
//Vibrate device
Utils.vibrate(getApplicationContext());
//get all playlists
Cursor playListCursor = AppController.getGlobalContentResolvere().query(
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, new String[]{"*"}, null, null,
null);
long playlistId = 0;
playListCursor.moveToFirst();
do {
//check if selected playlsit already exist
if (playListCursor.getString(playListCursor
.getColumnIndex(MediaStore.Audio.Playlists.NAME)).
equalsIgnoreCase(playlistName)) {
playlistId = playListCursor.getLong(playListCursor
.getColumnIndex(MediaStore.Audio.Playlists._ID));
break;
}
} while (playListCursor.moveToNext());
//Playlist doesnt exist creating new with given name
if (playlistId == 0) {
Log.d(TAG, "CREATING PLAYLIST: " + playlistName);
ContentValues playlisrContentValue = new ContentValues();
//Add name
playlisrContentValue.put(MediaStore.Audio.Playlists.NAME, playlistName);
//update modified value
playlisrContentValue.put(MediaStore.Audio.Playlists.DATE_MODIFIED,
System.currentTimeMillis());
Uri playlistURl = AppController.getGlobalContentResolvere().insert(
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, playlisrContentValue);
Log.d(TAG, "Added PlayLIst: " + playlistURl);
} else {
//Playlist alreay exist add to playlist
String[] cols = new String[]{
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
Cursor favListCursor = AppController.getGlobalContentResolvere().query(uri, cols, null, null, null);
favListCursor.moveToFirst();
final int base = favListCursor.getInt(0);
//playlist updated delete older playlist art so that we can create new
Toast.makeText(AudioPlayerActivity.this, "deleted old file" + new File(AppContants.PLAY_LIST_DIR + playlistId + ".png").delete(), Toast.LENGTH_SHORT).show();
favListCursor.close();
//add song to last
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base + songID);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songID);
AppController.getGlobalContentResolvere().insert(uri, values);
//Debug purpose
Toast.makeText(AudioPlayerActivity.this, "Added to Favourite list " +
CenterRepository.getInstance().getAudioCollection().getSongAt(AppConfig.SONG_NUMBER).getTitle()
, Toast.LENGTH_SHORT).show();
}
}
精彩评论