我试图找出一个MediaStore
查询来获取Playlist
的所有成员歌曲。但是,List
始终返回空,但没有结果(即使在通知适配器之后)。这就是我目前正在做的......
获取播放列表歌曲方法
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID);
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int idColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Playlists.Members.AUDIO_ID);
int titleColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Playlists.Members.TITLE);
int artistColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Playlists.Members.ARTIST);
int albumColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Playlists.Members.ALBUM);
int pathColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Playlists.Members.DATA);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisAlbum = musicCursor.getString(albumColumn);
String thisPath = musicCursor.getString(pathColumn);
songList.add(new Song(thisId, thisTitle, thisArtist, thisAlbum, thisPath));
}
while (musicCursor.moveToNext());
}
musicCursor.close();
以下是我如何使用它的ID将值添加到播放列表中...
添加播放列表歌曲方法
public static void addToPlaylist(ContentResolver resolver, long audioId, String title, String artist, String album, String path) {
String[] cols = new String[] {
"count(*)"
};
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID);
Cursor cur = resolver.query(uri, cols, null, null, null);
cur.moveToFirst();
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
values.put(MediaStore.Audio.Playlists.Members.TITLE, title);
values.put(MediaStore.Audio.Playlists.Members.ARTIST, artist);
values.put(MediaStore.Audio.Playlists.Members.ALBUM, album);
values.put(MediaStore.Audio.Playlists.Members.DATA, path);
resolver.insert(uri, values);
}
有什么建议吗?谢谢!
答案 0 :(得分:0)
对不起,我花了一点时间去度假。 要添加到播放列表,您只需要audio_id和play_order以及playlist_id。下面的代码有光标和where子句,因为这允许你添加多个轨道但是如果你已经有audio_id则不需要它
ContentResolver resolver = context.getContentResolver();
Uri exturi = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlist_id);
where = MediaStore.Audio.Media._ID + "=?";
String[] whereVal = {music_id};
Uri mediauri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor c = resolver.query(mediauri, projection, where, whereVal,
null);
if (c != null && c.moveToFirst()) {
int idColumn = c.getColumnIndex(MediaStore.Audio.Media._ID);
ContentValues values = new ContentValues();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String audio_id = c.getString(idColumn);
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);
resolver.insert(exturi, values);
base++;
}
c.close();
}
要获取播放列表中的所有曲目,我有以下代码:
public Cursor getPlaylistTracks(Context context, Long playlist_id) {
Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlist_id);
ContentResolver resolver = context.getContentResolver();
String _id = MediaStore.Audio.Playlists.Members._ID;
String audio_id = MediaStore.Audio.Playlists.Members.AUDIO_ID;
String artist = MediaStore.Audio.Playlists.Members.ARTIST;
String album = MediaStore.Audio.Playlists.Members.ALBUM;
String album_id = MediaStore.Audio.Playlists.Members.ALBUM_ID;
String title = MediaStore.Audio.Playlists.Members.TITLE;
String duration = MediaStore.Audio.Playlists.Members.DURATION;
String location = MediaStore.Audio.Playlists.Members.DATA;
String composer = MediaStore.Audio.Playlists.Members.COMPOSER;
String playorder = MediaStore.Audio.Playlists.Members.PLAY_ORDER;
String date_modified = MediaStore.Audio.Playlists.Members.DATE_MODIFIED;
String[] columns = {_id, audio_id, artist, album_id, album, title, duration,
location, date_modified, playorder, composer};
return resolver.query(newuri, columns, null, null, null);
}
我希望这会有所帮助