Android Create Playlist

后端 未结 4 1523
暗喜
暗喜 2020-12-23 10:58

Anyone know how to add playlists to Android in code?

I kind of get that I have to insert it into the content resolver but do I have to just put the song id in or do

4条回答
  •  被撕碎了的回忆
    2020-12-23 11:35

    Use this the code itself is self explanatory. It will add song with given id = songID to playlist with name playlistName

    If playlist already exist it will add to existing one or it will create new and then add song to it

     /**
         * This function add song with id songID to playlist playlistName
         * if playlist does exist it will add to exiixting one or it will create new
         *
         * @param playlistName
         * @param songID
         */
        private void addToPlaylist(String playlistName, int songID) {
    
            //Vibrate device
            Utils.vibrate(getApplicationContext());
    
            //get all playlists
            Cursor playListCursor = AppController.getGlobalContentResolvere().query(
                    MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, new String[]{"*"}, null, null,
                    null);
    
            long playlistId = 0;
    
            playListCursor.moveToFirst();
    
            do {
    
                //check if selected playlsit already exist
                if (playListCursor.getString(playListCursor
                        .getColumnIndex(MediaStore.Audio.Playlists.NAME)).
                        equalsIgnoreCase(playlistName)) {
    
                    playlistId = playListCursor.getLong(playListCursor
                            .getColumnIndex(MediaStore.Audio.Playlists._ID));
                    break;
                }
            } while (playListCursor.moveToNext());
    
            //Playlist  doesnt exist creating new with given name
            if (playlistId == 0) {
    
                Log.d(TAG, "CREATING PLAYLIST: " + playlistName);
    
                ContentValues playlisrContentValue = new ContentValues();
    
                //Add name
                playlisrContentValue.put(MediaStore.Audio.Playlists.NAME, playlistName);
    
                //update modified value
                playlisrContentValue.put(MediaStore.Audio.Playlists.DATE_MODIFIED,
                        System.currentTimeMillis());
    
                Uri playlistURl = AppController.getGlobalContentResolvere().insert(
                        MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, playlisrContentValue);
    
                Log.d(TAG, "Added PlayLIst: " + playlistURl);
    
            } else {
    
                //Playlist alreay exist add to playlist
                String[] cols = new String[]{
                        "count(*)"
                };
    
                Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    
                Cursor favListCursor = AppController.getGlobalContentResolvere().query(uri, cols, null, null, null);
    
                favListCursor.moveToFirst();
    
                final int base = favListCursor.getInt(0);
    
                //playlist updated delete older playlist art so that we can create new
                Toast.makeText(AudioPlayerActivity.this, "deleted old file" + new File(AppContants.PLAY_LIST_DIR + playlistId + ".png").delete(), Toast.LENGTH_SHORT).show();
    
                favListCursor.close();
    
                //add song to last
                ContentValues values = new ContentValues();
    
                values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base + songID);
    
                values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songID);
    
                AppController.getGlobalContentResolvere().insert(uri, values);
    
    
                //Debug purpose
                Toast.makeText(AudioPlayerActivity.this, "Added to Favourite list " +
                                CenterRepository.getInstance().getAudioCollection().getSongAt(AppConfig.SONG_NUMBER).getTitle()
                        , Toast.LENGTH_SHORT).show();
    
            }
        }
    

提交回复
热议问题