Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8237347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:26:40+00:00 2026-06-07T19:26:40+00:00

I’m attempting to create a new Playlist , using Android’s ContentResolver , that will

  • 0

I’m attempting to create a new Playlist, using Android’s ContentResolver, that will be added to music player’s playlists, but when the code runs, the insert into the playlist returns null (for the Uri) and when I check the music player’s playlists, my new Playlist entry isn’t there. I suspect that the reason that the insert() returns null is because I haven’t created the Playlist correctly. Could someone clarify how to dynamically create a new playlist, given that my code doesn’t work. (In my searching, I’ve found several way to query playlists, but nothing actually creates a new one)

Here’s my code…

    ContentResolver resolver = getActivity().getContentResolver();

    Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

    Log.d(A.TAG, "Checking for existing playlist for " + chart.getName());
    Cursor c = resolver.query(playlists, new String[] {"*"}, null, null, null);
    long playlistId = 0;
    c.moveToFirst();
    do {
        String plname = c.getString(c.getColumnIndex(MediaStore.Audio.Playlists.NAME));
        if (plname.equalsIgnoreCase(chart.getName())) {
            playlistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Playlists._ID));
            break;
        }
    } while (c.moveToNext());
    c.close();

    if (playlistId!=0) {
        Uri deleteUri = ContentUris.withAppendedId(playlists, playlistId);
        Log.d(A.TAG, "REMOVING Existing Playlist: " + playlistId);

        // delete the playlist
        resolver.delete(deleteUri, null, null);
    }

    Log.d(A.TAG, "CREATING PLAYLIST: " + chart.getName());
    ContentValues v = new ContentValues();
    v.put(MediaStore.Audio.Playlists.NAME, chart.getName());
    v.put(MediaStore.Audio.Playlists.DATE_MODIFIED, System.currentTimeMillis());
    Uri newpl = resolver.insert(playlists, v);
    Log.d(A.TAG, "Added PlayLIst: " + newpl);

    Uri insUri = Uri.withAppendedPath(newpl, MediaStore.Audio.Playlists.Members.CONTENT_DIRECTORY);

    int order = 1;
    Log.d(A.TAG, "Playlist Members Url: " + insUri);
    c = getContentManager().queryWhere(getActivity(), Song.class, Song.Fields.LIBRARYID + " != 0 and " + Song.Fields.CHARTID + " = " + chart.getId(), (String[])null);
    if (c.moveToFirst()) {
        Log.d(A.TAG, "Adding Songs to PlayList **");
        do {
            long id = c.getLong(c.getColumnIndex(Song.Fields.LIBRARYID));
            ContentValues cv = new ContentValues();
            cv.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, order++);
            cv.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, id);
            Uri u = resolver.insert(insUri, cv);
            Log.d(A.TAG, "Added Playlist Item: " + u + " for AUDIO_ID " + id);
        } while (c.moveToNext());
    }
    c.close();

UPDATE: Partially Solved **

The above code does correctly add a new Playlist on 4.0.3, but not on 2.3. The only problem areas for 4.0.3 was that I needed to make sure the DATE_MODIFIED was set on the Playlist and that PLAY_ORDER was set on the Playlist item.

I still have no idea why it would not create a playlist on 2.x, so if anyone has any thoughts on that, I’d like to know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:26:42+00:00Added an answer on June 7, 2026 at 7:26 pm

    This is the code I use in a custom built AsyncTask and it works on 2.3, 3.1, and 4.03:

                ContentValues mInserts = new ContentValues();
                mInserts.put(MediaStore.Audio.Playlists.NAME, mPrefs.getString(AM.MEDIASTORECHANGE_NEWPLAYLISTNAME, "New Playlist"));
                mInserts.put(MediaStore.Audio.Playlists.DATE_ADDED, System.currentTimeMillis());
                mInserts.put(MediaStore.Audio.Playlists.DATE_MODIFIED, System.currentTimeMillis());
                mUri = mCR.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, mInserts);
                if (mUri != null) {
                    mPlaylistId = -1;
                    mResult = FM.SUCCESS;
                    c = mCR.query(mUri, PROJECTION_PLAYLIST, null, null, null);
                    if (c != null) {
                        // Save the newly created ID so it can be selected.  Names are allowed to be duplicated,
                        // but IDs can never be.
                        mPlaylistId = c.getInt(c.getColumnIndex(MediaStore.Audio.Playlists._ID));
                        c.close();
                    }
                    if (DBG.AUDIO) {
                        Log.d(TAG, "PLAYLIST_ADD - mPlaylistId: " + mPlaylistId 
                                + "  mSelectString: " + mSelectString + "  mUri: "+ mUri.toString());
                    }
    
                }
    
    public static final String[] PROJECTION_PLAYLIST = new String[] {
        MediaStore.Audio.Playlists._ID,
        MediaStore.Audio.Playlists.NAME,
        MediaStore.Audio.Playlists.DATA
    };
    

    To get the correct Uri to add playlist members, you need the Id. To add songs to the playlist, you also need to know the current high water mark of PLAYORDER in the playlist’s current state. Otherwise the MediaStore ContentResolver will gag because you are trying to insert playlist members with the same play order. So, you need to query the Playlist Uri first to get the highest PLAYORDER value, and use that as the starting point for your ContentValues inserts.

    I have only tried inserting one playlist member at a time, though in theory you might be able to do a bulk insert. The code below is set up to convert to a bulk insert in the future, but currently only does one insert at a time. It takes a cursor of MediaStore.Audio.Media songs and inserts them into a playlist Id that has been stored in SharedPreferences.

        private void addSongsInCursorToPlaylist(Cursor c) {
        int mIdCol;
        int mCount;
        int mPercent = 0;
        ContentResolver mCR = mContext.getContentResolver();
        ContentProviderClient mCRC = null;
        try {
            mCount = c.getCount();
            mIdCol = c.getColumnIndex(MediaStore.Audio.Media._ID);
            ContentValues[] mInsertList = new ContentValues[1];
            mInsertList[0] = new ContentValues();
            int mPlaylistId  = mPrefs.getInt(AM.PLAYLIST_NOWPLAYING_ID, AM.PLAYLIST_ID_DEFAULT);
            Uri mUri = MediaStore.Audio.Playlists.Members.getContentUri("external", mPlaylistId);
            Cursor c2 = mCR.query(mUri, 
                    PROJECTION_PLAYLISTMEMBERS_PLAYORDER, null, null, MediaStore.Audio.Playlists.Members.PLAY_ORDER + " DESC ");
            int mPlayOrder = 1;
            if (c2 != null) {
                if (c2.moveToFirst()) {
                    mPlayOrder = (c2.getInt(c2.getColumnIndex(MediaStore.Audio.Playlists.Members.PLAY_ORDER))) + 1;
                }
                c2.close();
            }
            mCRC = mCR.acquireContentProviderClient(mUri);
            if (DBG.AUDIO) {
                Log.d(TAG, "addSongsInCursorToPlaylist -Content Uri: " + mUri.toString() 
                        + "  PlaylistID: " + mPlaylistId + " mPlayOrder: " + mPlayOrder);
            }
            for (int i=0; i< mCount; i++) {
                if (c.moveToPosition(i)) {
                    // Don't pollute with progress messages..has to be at least 1% increments
                    int mTemp = (i * 100) / (mCount );
                    if (mTemp > mPercent) {
                        mPercent = mTemp;
                        publishProgress(mPercent);
                    }
                    mInsertList[0].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, c.getLong(mIdCol));
                    mInsertList[0].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, mPlayOrder++);
                    mCR.insert(mUri, mInsertList[0]);
                    if (DBG.AUDIO) {
                        Log.d(TAG, "addSongsInCursorToPlaylist -Adding AudioID: " + c.getLong(mIdCol) + " to Uri: " + mUri.toString()  );
                    }
                }
                mCRC.release();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    
    }
        // Projection to get high water mark of PLAY_ORDER in a particular playlist
    public static final String[] PROJECTION_PLAYLISTMEMBERS_PLAYORDER = new String[] {
        MediaStore.Audio.Playlists.Members._ID,
        MediaStore.Audio.Playlists.Members.PLAY_ORDER
    };
    // Projection to get the list of song IDs to be added to a playlist
    public static final String[] PROJECTION_SONGS_ADDTOPLAYLIST = new String[] {
        MediaStore.Audio.Media._ID,
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.