任何人都知道如何在代码中向Android添加播放列表?
我知道我必须将其插入到内容解析器中,但是我必须将歌曲ID放入或者是否必须将所有歌曲的数据放入其中?
我一直在寻找示例代码,但还没有找到。
编辑: 在这里找到答案是我如何做到的:
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);
}
答案 0 :(得分:12)
为了解决“没有回答”的问题,我在这里粘贴了OP的代码:
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);
}
答案 1 :(得分:2)
要回答JaroslavZáruba的评论,使用添加轨道设置的PLAY_ORDER代码会更好,如下所示:
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));
我们使用播放列表的最后一个元素(cur.moveToLast()),并在其PLAY_ORDER值中加1,以确定新曲目的PLAY_ORDER。关键是在播放列表中有连续的曲目。
您还可以添加10例如,以便在新曲目之前或之后插入曲目。 我也改变了我们获得轨道id的方式。实际上,我们不希望在获取错误数据时遇到任何问题,因此我们指定了我们想要的列。
答案 2 :(得分:0)
这是从播放列表中删除歌曲的更正代码:
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);
}
答案 3 :(得分:0)
使用此代码本身是不言自明的。它会将给定id = songID
的歌曲添加到名称为playlistName
如果播放列表已经存在,它将添加到现有播放列表,或者它将创建新歌曲,然后添加歌曲
/**
* 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();
}
}