How to create a playlist

I am trying to create an application that just offers edittext and imagebutton. If you press the butten button, the idea is that the album is added to the playlist named in the edittext window. Albums should be randomly selected. It goes without saying that the album tracks must be in the correct order. I can add additional features later, for example. save, overwrite, delete, etc. I have an interface, but I'm struggling with code. I kind of understand the concept of ContentProviders. therefore, the code should:

  • access to playlists, which, it seems to me, are achieved using MediaStore.Audio.Playlists

  • access to albums, which, it seems to me, are achieved using MediaStore.Audio. Albums.

  • add to playlist

I have the following code (most bits received 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; } 

Who can help?

+4
source share
3 answers

I think you mean NullPointerException , which means that one of your assignments is null, and then you try to access the member of the object that you intended it to. This is most likely a resolver , but you need to specify the line number and / or execute it using the debugger.

+1
source

It works. When using ContentResolver, context is required (this).

 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; } 
+1
source

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; } 
+1
source

All Articles