Android 创建播放列表
有人知道如何用代码向 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了解决“未回答”的问题,我将OP的代码粘贴在这里:
To get the question out of "not answered" I have pasted the OP's code here:
为了回答 Jaroslav Záruba 的评论,代码最好使用添加的曲目集的 PLAY_ORDER,如下所示:
有两个主要变化:我们使用播放列表的最后一个元素 (cur.moveToLast()),并将 1 添加到其 PLAY_ORDER 值来确定新曲目的 PLAY_ORDER。重点是播放列表中要有连续的曲目。
例如,您还可以添加 10 个,以便可以在新曲目之前或之后插入曲目。
我还改变了获取曲目 ID 的方式。事实上,我们不希望出现获取错误数据的任何问题,因此我们指定了所需的列。
To answer Jaroslav Záruba comment, the code is better with the PLAY_ORDER of added track set as follows:
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:
使用这个代码本身是不言自明的。它将把给定的
id = SongID
的歌曲添加到名为playlistName
的播放列表中。如果播放列表已存在,它将添加到现有播放列表,或者创建新的播放列表,然后将歌曲添加到其中
Use this the code itself is self explanatory. It will add song with given
id = songID
to playlist with nameplaylistName
If playlist already exist it will add to existing one or it will create new and then add song to it