我正在尝试从MediaStore查询设备中的播放列表。我之前提出过一个问题,但我没有得到答案。
这是我查询播放列表的方式
public void addToPlaylist(long playlistId, Context context,
ArrayList<Play> playlistTracks, String playlistName) {
int count = getPlaylistSize(playlistId, context);
Log.d("playlist size=", "" + count);
ContentValues[] values = new ContentValues[playlistTracks.size()];
for (int i = 0; i < playlistTracks.size(); i++) {
values[i] = new ContentValues();
values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i
+ count + 1);
values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID,
playlistTracks.get(i).getId());
values[i].put(MediaStore.Audio.Playlists.NAME, playlistName);
}
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
playlistId);
int num = resolver.bulkInsert(uri, values);
Log.d("songs added=", "" + num); // added
resolver.notifyChange(Uri.parse("content://media"), null);
}
private int getPlaylistSize(long playlistId, Context context)
{
// TODO: Implement this method
return 0;
}
public void retrieveAllPlaylists(Context context) {
Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String idKey = MediaStore.Audio.Playlists._ID;
final String nameKey = MediaStore.Audio.Playlists.NAME;
final String[] columns = { idKey, nameKey };
Cursor playListCursor = context.getContentResolver().query(
tempPlaylistURI, columns, null, null, null);
if (playListCursor != null) {
Log.d("playlist cursor count=", "" + playListCursor.getCount());
for (boolean hasItem = playListCursor.moveToFirst(); hasItem; hasItem = playListCursor
.moveToNext()) {
playlistName = playListCursor.getString(playListCursor
.getColumnIndex(nameKey));
// noOfTracks = playListCursor.getInt(playListCursor
// .getColumnIndex(tracksCountKey));
Log.d(this.getClass().getName(), "playlistname=" + playlistName // returns only default playliststhe
+ "tracks=" + noOfTracks);
playlistModel.add(new Play(playlistName, noOfTracks));
}
}
}
我得到的只是一个空白屏幕。请指导我
答案 0 :(得分:2)
创建播放列表的简单方法是:
public void addnewPlaylist(Context context, String newplaylist) {
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues(1);
values.put(MediaStore.Audio.Playlists.NAME, newplaylist);
resolver.insert(uri, values);
}
然后填充播放列表:
public void addTrackToPlaylist(Context context, String audio_id,
long playlist_id, int pos) {
Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlist_id);
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, pos);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);
values.put(MediaStore.Audio.Playlists.Members.PLAYLIST_ID,
playlist_id);
resolver.insert(newuri, values);
}
查看代码,您不会构建正确的结构。播放列表不包含播放列表名称,只包含Playlist_id。
要返回所有播放列表:
public Cursor getandroidPlaylistcursor(Context context) {
ContentResolver resolver = context.getContentResolver();
final String id = MediaStore.Audio.Playlists._ID;
final String name = MediaStore.Audio.Playlists.NAME;
final String[] columns = { id, name };
final String criteria = null
return resolver.query(uri, columns, criteria, null,
name + " ASC");
}
答案 1 :(得分:1)
回答kh3e的问题: 在处理播放列表和播放列表成员时
public Uri get_audio_playlists_uri(){
Uri uri_to_use = null;
// int bld = Build.VERSION.SDK_INT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
uri_to_use = MediaStore.Audio.Playlists.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
uri_to_use = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
}
return uri_to_use;
}
public Uri get_audio_playlist_members_uri(long playlist_id){
Uri uri_to_use = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
uri_to_use = MediaStore.Audio.Playlists.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
.buildUpon()
.appendEncodedPath(Long.toString(playlist_id))
.appendEncodedPath("members")
.build();
} else {
uri_to_use = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI
.buildUpon()
.appendEncodedPath(Long.toString(playlist_id))
.appendEncodedPath("members")
.build();
}
return uri_to_use;
}
答案 2 :(得分:0)
填充数组的示例:
Cursor cursor = plist.getPlaylistTracks(getActivity(), playlist_id);
// replace with your own method to get cursor
ArrayList<String> audio_ids = new ArrayList<String>();
// build up the array with audio_id's
int i = 0;
if (cursor != null && cursor.moveToFirst()) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String audio_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID));
audio_ids.add(audio_id);
}
确保在分配audio_id(或您自己的变量)时,使用获取光标时使用的正确MediaStore uri。光标中的列名确定了您可以在此处获得的内容。