gpt4 book ai didi

android - 从 MediaStore 查询播放列表

转载 作者:行者123 更新时间:2023-12-05 00:11:25 27 4
gpt4 key购买 nike

我正在尝试从 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));
}


}

}

我得到的只是一个空白屏幕。请指导我

最佳答案

创建播放列表的简单方法是:

            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");

}

关于android - 从 MediaStore 查询播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com