Querying Playlist from MediaStore

匆匆过客 提交于 2019-12-01 01:45:12

the simple way to create a playlist is :

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

}

then populate the playlist:

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

}

Looking at your code, you do not build the correct structure. A playlist does not hold the playlist name, simply the Playlist_id.

To return all your playlists :

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

}

Example to populate an array :

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

Make sure that when you assign audio_id (or your own variable) you use the correct MediaStore uri as used when you obtained the cursor. The column names in your cursor determine what you can get here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!