/** 批量向歌单添加音乐 */
  public static void addSongListToPlaylist(
      final Context context, final PlayList playlist, final ArrayList<Song> songs) {
    Cursor cur =
        context
            .getContentResolver()
            .query(
                MediaStore.Audio.Playlists.Members.getContentUri(
                    "external", playlist.getmPlayListId()),
                null,
                null,
                null,
                MediaStore.Audio.Playlists.Members.TRACK + " ASC");

    long count = 0;
    if (cur.moveToLast()) {
      count = cur.getLong(cur.getColumnIndex(MediaStore.Audio.Playlists.Members.TRACK));
    }
    cur.close();

    ContentValues[] values = new ContentValues[songs.size()];
    for (int i = 0; i < songs.size(); i++) {
      values[i] = new ContentValues();
      values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, count + 1);
      values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songs.get(i).getmSongId());
    }

    Uri uri =
        MediaStore.Audio.Playlists.Members.getContentUri("external", playlist.getmPlayListId());
    ContentResolver resolver = context.getContentResolver();
    resolver.bulkInsert(uri, values);
    resolver.notifyChange(Uri.parse("content://media"), null);
  }
  /** 向歌单添加音乐 */
  public static void addSongToPlaylist(
      final Context context, final PlayList playlist, final Song song) {
    Cursor cur =
        context
            .getContentResolver()
            .query(
                MediaStore.Audio.Playlists.Members.getContentUri(
                    "external", playlist.getmPlayListId()),
                null,
                null,
                null,
                MediaStore.Audio.Playlists.Members.TRACK + " ASC");

    long count = 0;
    if (cur.moveToLast()) {
      count = cur.getLong(cur.getColumnIndex(MediaStore.Audio.Playlists.Members.TRACK));
    }
    cur.close();

    ContentValues values = new ContentValues();
    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, count + 1);
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, song.getmSongId());

    Uri uri =
        MediaStore.Audio.Playlists.Members.getContentUri("external", playlist.getmPlayListId());
    ContentResolver resolver = context.getContentResolver();
    resolver.insert(uri, values);
    resolver.notifyChange(Uri.parse("content://media"), null);
  }
  public static List<Song> getAllSongsFromPlaylist(
      ContentResolver contentResolver, long playlistId) {
    String[] projection = {
      MediaStore.Audio.Playlists.Members._ID,
      MediaStore.Audio.Playlists.Members.DATA,
      MediaStore.Audio.Playlists.Members.ARTIST,
      MediaStore.Audio.Playlists.Members.TITLE,
      MediaStore.Audio.Playlists.Members.ALBUM,
      MediaStore.Audio.Playlists.Members.DURATION,
      MediaStore.Audio.Playlists.Members.PLAY_ORDER
    };

    Cursor cursor =
        contentResolver.query(
            MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
            projection,
            null,
            null,
            MediaStore.Audio.Playlists.Members.PLAY_ORDER);

    if (cursor != null) {
      List<Song> songs = new ArrayList<Song>();
      while (cursor.moveToNext()) {
        songs.add(new Song(cursor));
      }

      return songs;
    }

    return null;
  }
  public static void addAlbumToPlaylist(ContentResolver contentResolver, long playlistId, long id) {
    String[] projection = {
      MediaStore.Audio.Media._ID,
    };

    String selection = MediaStore.Audio.Media.ALBUM_ID + "=" + id;

    Cursor cursor =
        contentResolver.query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            MediaStore.Audio.Media.TRACK);

    if (cursor != null) {
      int base = getLastTrackNumber(contentResolver, playlistId) + 1;

      while (cursor.moveToNext()) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, cursor.getLong(0));
        contentValues.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base);
        base++;

        contentResolver.insert(
            MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
            contentValues);
      }
    }
  }
  /** 获得播放清单里的所有歌曲 */
  public static ArrayList<Song> getPlayListSongs(Context context, PlayList playList) {
    ArrayList<Song> songs = new ArrayList<>();

    Cursor cursor =
        context
            .getContentResolver()
            .query(
                MediaStore.Audio.Playlists.Members.getContentUri(
                    "external", playList.getmPlayListId()),
                playListSongProjection,
                MediaStore.Audio.Media.IS_MUSIC + " != 0",
                null,
                null);

    for (int i = 0; i < cursor.getCount(); i++) {
      cursor.moveToPosition(i);
      Song song =
          new Song(
              cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID)),
              cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.TITLE)),
              cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ARTIST)),
              cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ALBUM)),
              cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.DURATION)),
              cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.DATA)),
              cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ALBUM_ID)),
              cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ARTIST_ID)));
      songs.add(song);
    }

    cursor.close();
    return songs;
  }
 /**
  * Builds a query that will return all the songs in the playlist with the given id.
  *
  * @param id The id of the playlist in MediaStore.Audio.Playlists.
  * @param projection The columns to query.
  * @param selection The selection to pass to the query, or null.
  * @return The initialized query.
  */
 public static QueryTask buildPlaylistQuery(long id, String[] projection, String selection) {
   Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", id);
   String sort = MediaStore.Audio.Playlists.Members.PLAY_ORDER;
   QueryTask result = new QueryTask(uri, projection, selection, null, sort);
   result.type = TYPE_PLAYLIST;
   return result;
 }
 /**
  * Creates the {@link Cursor} used to run the query.
  *
  * @param context The {@link Context} to use.
  * @param playlistID The playlist the songs belong to.
  * @return The {@link Cursor} used to run the song query.
  */
 public static final Cursor makePlaylistSongCursor(
     final Context context, final String playlistID) {
   final StringBuilder mSelection = new StringBuilder();
   mSelection.append(AudioColumns.IS_MUSIC + "=1");
   mSelection.append(" AND " + AudioColumns.TITLE + " != ''"); // $NON-NLS-2$
   return context
       .getContentResolver()
       .query(
           MediaStore.Audio.Playlists.Members.getContentUri("external", Long.valueOf(playlistID)),
           new String[] {
             /* 0 */
             MediaStore.Audio.Playlists.Members._ID,
             /* 1 */
             MediaStore.Audio.Playlists.Members.AUDIO_ID,
             /* 2 */
             AudioColumns.TITLE,
             /* 3 */
             AudioColumns.ARTIST,
             /* 4 */
             AudioColumns.ALBUM
           },
           mSelection.toString(),
           null,
           MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);
 }
 public static void addToPlaylist(Context context, long[] ids, long playlistid) {
   if (ids == null) {
     // this shouldn't happen (the menuitems shouldn't be visible
     // unless the selected item represents something playable
     Log.e("MusicBase", "ListSelection null");
   } else {
     int size = ids.length;
     ContentResolver resolver = context.getContentResolver();
     // need to determine the number of items currently in the playlist,
     // so the play_order field can be maintained.
     String[] cols = new String[] {"count(*)"};
     Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistid);
     Cursor cur = resolver.query(uri, cols, null, null, null);
     cur.moveToFirst();
     int base = cur.getInt(0);
     cur.close();
     int numinserted = 0;
     for (int i = 0; i < size; i += 1000) {
       makeInsertItems(ids, i, 1000, base);
       numinserted += resolver.bulkInsert(uri, sContentValuesCache);
     }
     String message =
         context
             .getResources()
             .getQuantityString(R.plurals.NNNtrackstoplaylist, numinserted, numinserted);
     Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
     // mLastPlaylistSelected = playlistid;
   }
 }
 /** {@inheritDoc} */
 @Override
 public void remove(final int which) {
   mSong = mAdapter.getItem(which - 1);
   mAdapter.remove(mSong);
   mAdapter.notifyDataSetChanged();
   final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", mPlaylistId);
   getActivity()
       .getContentResolver()
       .delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID + "=" + mSong.mSongId, null);
 }
  public static void removeFromPlaylist(ContentResolver resolver, int audioId, long playlistId) {
    String[] cols = new String[] {"count(*)"};
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    Cursor cur = resolver.query(uri, cols, null, null, null);
    cur.moveToFirst();
    final int base = cur.getInt(0);
    cur.close();
    ContentValues values = new ContentValues();

    resolver.delete(uri, MediaStore.Audio.Playlists.Members.AUDIO_ID + "=" + audioId, null);
  }
 // Adds the specified song to Android's MediaStore.
 public static void addToMediaStorePlaylist(
     ContentResolver resolver, int audioId, long playlistId) {
   String[] cols = new String[] {"count(*)"};
   Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
   Cursor cur = resolver.query(uri, cols, null, null, null);
   cur.moveToFirst();
   final int base = cur.getInt(0);
   cur.close();
   ContentValues values = new ContentValues();
   values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base + audioId));
   values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId);
   resolver.insert(uri, values);
 }
  public static long[] getSongListForPlaylist(Context context, long plid) {
    final String[] ccols = new String[] {MediaStore.Audio.Playlists.Members.AUDIO_ID};
    Cursor cursor =
        query(
            context,
            MediaStore.Audio.Playlists.Members.getContentUri("external", plid),
            ccols,
            null,
            null,
            MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER);

    if (cursor != null) {
      long[] list = getSongListForCursor(cursor);
      cursor.close();
      return list;
    }
    return sEmptyList;
  }
  public static int getLastTrackNumber(ContentResolver contentResolver, long playlistId) {
    String[] projection = {
      MediaStore.Audio.Playlists.Members._ID, MediaStore.Audio.Playlists.Members.PLAY_ORDER
    };

    String selection = MediaStore.Audio.Playlists.Members.PLAYLIST_ID + "=" + playlistId;

    Cursor cursor =
        contentResolver.query(
            MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
            projection,
            selection,
            null,
            MediaStore.Audio.Playlists.Members.PLAY_ORDER);

    if (cursor != null && cursor.moveToLast()) {
      return cursor.getInt(1);
    } else {
      return -1;
    }
  }
 public static void clearPlaylist(ContentResolver contentResolver, long playlistId) {
   contentResolver.delete(
       MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId), null, null);
 }
  // Playlist member retriever
  public ArrayList<SongItem> getSongsInPlaylist(PlaylistItem pl) {
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", pl.id);

    return queryForSongs(uri, null, MediaStore.Audio.Media.IS_MUSIC + "=1", null, null);
  }
  public static void clearPlaylist(Context context, int plid) {

    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", plid);
    context.getContentResolver().delete(uri, null, null);
    return;
  }