How to Create a Playlist

5.8k views Asked by At

I am trying to create an app which simply offers an edittext and imagebutton. If the butten gets clicked, the idea is that an album is added to the Playlist, named in the edittext box. Albums should be selected randomly. Goes without saying that the album tracks should be in the correct order. I can add more functionality later eg. save, overwrite, delete etc. I have the interface but am struggling with the code. I sort of get the concept of ContentProviders. so the code needs to:

  • access the Playlists, which I believe is achieved by using MediaStore.Audio.Playlists

  • access the Albums, which I believe is achieved by using MediaStore.Audio.Albums

  • add to the Playlist

I have the following code (most bits obtained from this site. Thanks btw) to access the Playlist but it crashes with a Null Exception error.

public void checkforplaylists()
{
    //Get a cursor over all playlists.
    final ContentResolver resolver= MediaProvider.mContentResolver;
    final Uri uri=MediaStore.Audio.Playlists.INTERNAL_CONTENT_URI;
    final String id=MediaStore.Audio.Playlists._ID;
    final String name=MediaStore.Audio.Playlists.NAME;
    final String[]columns={id,name};
    final Cursor playlists= resolver.query(uri, columns, null, null, null);

        if(playlists==null)
            {
                Log.e(TAG,"Found no playlists.");
                    return;
            }
    return;
}

Anyone who can help?

3

There are 3 answers

0
Issac Balaji On

use this code, will work

public boolean addPlaylist(String pname) {


    Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    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(pname)) {
            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(TAG, "REMOVING Existing Playlist: " + playlistId);

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

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

    flag=true;
    return flag;
}
0
CodeClown42 On

I think you mean NullPointerException, which means one of your assignments is null and then you try to access a member of the object you intended it to be. Most likely it is resolver, but to be sure you need the line number reported and/or to step through that with a debugger.

0
Theo On

This works. When using the ContentResolver, the Context (this) is required.

public void checkforplaylists(Context context)
{
    ContentResolver cr = context.getContentResolver();
    final Uri uri=MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    final String id=MediaStore.Audio.Playlists._ID;
    final String name=MediaStore.Audio.Playlists.NAME;
    final String[]columns={id,name};
    final Cursor playlists= cr.query(uri, columns, null, null, null);
        if(playlists==null)
            {
                Log.e(TAG,"Found no playlists.");
                    return;
            }
        Log.e(TAG,"Found playlists.");
    return;
}