Android 创建播放列表

发布于 2024-09-07 23:15:51 字数 1465 浏览 3 评论 0原文

有人知道如何用代码向 Android 添加播放列表吗?

我知道我必须将其插入到内容解析器中,但是我是否必须只将歌曲 ID 放入其中,还是必须将歌曲的所有数据放入其中?

我一直在寻找示例代码,但尚未找到。

编辑: 在这里找到了答案,我是这样做的:

  public static void addToPlaylist(ContentResolver resolver, int audioId) {

        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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 void removeFromPlaylist(ContentResolver resolver, int audioId) {
       Log.v("made it to add",""+audioId);
        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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);
    }

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 I have to put all of the song's data in?

I have been looking for sample code but haven't found any yet.

EDIT:
Found an answer here is how I do it:

  public static void addToPlaylist(ContentResolver resolver, int audioId) {

        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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 void removeFromPlaylist(ContentResolver resolver, int audioId) {
       Log.v("made it to add",""+audioId);
        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光无声 2024-09-14 23:15:51

为了解决“未回答”的问题,我将OP的代码粘贴在这里:

 public static void addToPlaylist(ContentResolver resolver, int audioId) {

        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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 void removeFromPlaylist(ContentResolver resolver, int audioId) {
       Log.v("made it to add",""+audioId);
        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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);
    }

To get the question out of "not answered" I have pasted the OP's code here:

 public static void addToPlaylist(ContentResolver resolver, int audioId) {

        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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 void removeFromPlaylist(ContentResolver resolver, int audioId) {
       Log.v("made it to add",""+audioId);
        String[] cols = new String[] {
                "count(*)"
        };
        Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
        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);
    }
淡淡的优雅 2024-09-14 23:15:51

为了回答 Jaroslav Záruba 的评论,代码最好使用添加的曲目集的 PLAY_ORDER,如下所示:

cur.moveToLast();
final int base = cur.getInt(cur.getColumnIndex(Playlists.Members.PLAY_ORDER));
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, 
      Integer.valueOf(base + 1));

有两个主要变化:我们使用播放列表的最后一个元素 (cur.moveToLast()),并将 1 添加到其 PLAY_ORDER 值来确定新曲目的 PLAY_ORDER。重点是播放列表中要有连续的曲目。

例如,您还可以添加 10 个,以便可以在新曲目之前或之后插入曲目。
我还改变了获取曲目 ID 的方式。事实上,我们不希望出现获取错误数据的任何问题,因此我们指定了所需的列。

To answer Jaroslav Záruba comment, the code is better with the PLAY_ORDER of added track set as follows:

cur.moveToLast();
final int base = cur.getInt(cur.getColumnIndex(Playlists.Members.PLAY_ORDER));
cur.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, 
      Integer.valueOf(base + 1));

Two major things change : We use the last element of the playlist (cur.moveToLast()) and we add 1 to its PLAY_ORDER value to determine the new track's PLAY_ORDER. The point is to have successive tracks in the playlist.

You can also add 10 for example so that you can insert tracks before or after your new track.
I also changed the way we get the id of track. Indeed we don't want to have any problem getting wrong data so we specify the column we want.

行雁书 2024-09-14 23:15:51

这是从播放列表中删除歌曲的更正代码:

public static void removeFromPlaylist(ContentResolver resolver, int audioId) 
{
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
resolver.delete(uri, MediaStore.Audio.Playlists.Members._ID +" = "+audioId, null);
}

This is corrected code which remove song from Playlist:

public static void removeFromPlaylist(ContentResolver resolver, int audioId) 
{
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", YOUR_PLAYLIST_ID);
resolver.delete(uri, MediaStore.Audio.Playlists.Members._ID +" = "+audioId, null);
}
2024-09-14 23:15:51

使用这个代码本身是不言自明的。它将把给定的 id = SongID 的歌曲添加到名为 playlistName 的播放列表中。

如果播放列表已存在,它将添加到现有播放列表,或者创建新的播放列表,然后将歌曲添加到其中

 /**
     * 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();

        }
    }

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();

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文