我正在尝试Playlist
使用 Android 创建一个新的ContentResolver
,它将添加到音乐播放器的播放列表中,但是当代码运行时,插入到播放列表中返回 null (对于Uri
),当我检查音乐播放器的播放列表时,我的新Playlist
条目不在那里。我怀疑insert()
返回 null 的原因是因为我没有正确创建播放列表。鉴于我的代码不起作用,有人可以澄清如何动态创建新的播放列表。(在我的搜索中,我找到了几种查询播放列表的方法,但实际上并没有创建新的方法)
这是我的代码...
ContentResolver resolver = getActivity().getContentResolver();
Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
Log.d(A.TAG, "Checking for existing playlist for " + chart.getName());
Cursor c = resolver.query(playlists, new String[] {"*"}, null, null, null);
long playlistId = 0;
c.moveToFirst();
do {
String plname = c.getString(c.getColumnIndex(MediaStore.Audio.Playlists.NAME));
if (plname.equalsIgnoreCase(chart.getName())) {
playlistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Playlists._ID));
break;
}
} while (c.moveToNext());
c.close();
if (playlistId!=0) {
Uri deleteUri = ContentUris.withAppendedId(playlists, playlistId);
Log.d(A.TAG, "REMOVING Existing Playlist: " + playlistId);
// delete the playlist
resolver.delete(deleteUri, null, null);
}
Log.d(A.TAG, "CREATING PLAYLIST: " + chart.getName());
ContentValues v = new ContentValues();
v.put(MediaStore.Audio.Playlists.NAME, chart.getName());
v.put(MediaStore.Audio.Playlists.DATE_MODIFIED, System.currentTimeMillis());
Uri newpl = resolver.insert(playlists, v);
Log.d(A.TAG, "Added PlayLIst: " + newpl);
Uri insUri = Uri.withAppendedPath(newpl, MediaStore.Audio.Playlists.Members.CONTENT_DIRECTORY);
int order = 1;
Log.d(A.TAG, "Playlist Members Url: " + insUri);
c = getContentManager().queryWhere(getActivity(), Song.class, Song.Fields.LIBRARYID + " != 0 and " + Song.Fields.CHARTID + " = " + chart.getId(), (String[])null);
if (c.moveToFirst()) {
Log.d(A.TAG, "Adding Songs to PlayList **");
do {
long id = c.getLong(c.getColumnIndex(Song.Fields.LIBRARYID));
ContentValues cv = new ContentValues();
cv.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, order++);
cv.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, id);
Uri u = resolver.insert(insUri, cv);
Log.d(A.TAG, "Added Playlist Item: " + u + " for AUDIO_ID " + id);
} while (c.moveToNext());
}
c.close();
更新:部分解决**
上面的代码确实正确地添加了一个新的播放列表 on 4.0.3
,但不是 on 2.3
。唯一的问题区域4.0.3
是我需要确保DATE_MODIFIED
已在播放列表中PLAY_ORDER
设置并且已在播放列表项中设置。
我仍然不知道为什么它不会在 2.x 上创建播放列表,所以如果有人对此有任何想法,我想知道。