2

I'm working on playlist in my audio player. I have return a code to add songs to playlist. The code works fine, but i have a small problem though. The problem is if I add a single song to playlist, two copies of the same song are added. It is like, if I add song A to playlist and then I open the playlist to which I added song A, I can see two copies of song A in there.

Code:

 public static void AddSongToPlaylist(long songID, long pID, Context context ){



    Uri pUri = MediaStore.Audio.Playlists.Members.getContentUri("external", pID);

    ContentResolver resolver = context.getContentResolver();
    ContentValues values = new ContentValues();

    String[] cols = new String[] {
            "count(*)"
    };
    Cursor cur = resolver.query(pUri, cols, null, null, null);
    cur.moveToFirst();
    final int base = cur.getInt(0)+1;
    cur.close();

    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER,base);
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songID);

    resolver.insert(pUri,values);
    resolver.notifyChange(Uri.parse("content://media"), null);
    Log.i("URI:",resolver.insert(pUri, values)+"");
    Toast.makeText(context, "Song Added", Toast.LENGTH_SHORT).show();
    Log.i("Song ID:", String.valueOf(songID));

}
Rektirino
  • 582
  • 5
  • 24

1 Answers1

1

Stop calling resolver.insert() again through Log.i().

Axe
  • 6,285
  • 3
  • 31
  • 38
  • Please give more explanation in your answer. It looks like you are right, but the wording of your answer is not very clear and it took me a minute to realize what you meant. – Feliks Montez Jun 15 '18 at 18:41