/** 批量向歌单添加音乐 */ 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 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; }
/** 删除歌单 */ public static PlayList deletePlaylist(final Context context, final PlayList playlist) { // 从MediaStore移除歌单 context .getContentResolver() .delete( MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, MediaStore.Audio.Playlists._ID + "=?", new String[] {playlist.getmPlayListId() + ""}); // 更新歌单 mPlayListLib.clear(); setPlayListLib(scanPlayList(context)); return playlist; }