Querying Playlist from MediaStore

后端 未结 3 1315
盖世英雄少女心
盖世英雄少女心 2021-01-07 06:33

I\'m trying to query playlists in the device from MediaStore. I have followed a question asked before but I didn\'t get the answer.

This is how I query for playlist

相关标签:
3条回答
  • 2021-01-07 06:42

    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");
    
    }
    
    0 讨论(0)
  • 2021-01-07 06:58

    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.

    0 讨论(0)
  • 2021-01-07 07:01

    to answer a question from kh3e: when dealing with playlists and playlist members

       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;
    }
    
    0 讨论(0)
提交回复
热议问题