从播放列表中检索时,Android歌曲ID与我从Media检索时不同

时间:2014-07-06 14:38:44

标签: android media android-contentresolver playlists

我有两种不同的方法可以使用内容解析器检索歌曲。一个我用来获取手机上的所有歌曲,一个用来从我的手机中检索所有播放列表,然后,对于每个播放列表我得到它上面的歌曲并将其添加到播放列表对象。我通过搜索与播放列表中的ID匹配的ID的所有歌曲列表来执行此操作。问题是ids不匹配。我肯定知道前任。一个播放列表上有3首歌曲,但它找到的ID与其中一首歌曲完全不同。 这是我用来检索所有歌曲的方法:

public ArrayList<Song> getSongList(){
        ArrayList<Song> songs = new ArrayList<Song>();
        //query external audio
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
        //iterate over results if valid
        if(musicCursor!=null && musicCursor.moveToFirst()){
            //get columns
            int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
            int albumColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM);
            int durationColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
            int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
            int displayNameColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DISPLAY_NAME);
            //add songs to list
            do {
                String name;
                if(musicCursor.getString(titleColumn)==null || musicCursor.getString(titleColumn).isEmpty()) name=musicCursor.getString(displayNameColumn);
                else name=musicCursor.getString(titleColumn);

                songs.add(new Song(musicCursor.getLong(idColumn), name, musicCursor.getString(albumColumn), musicCursor.getString(artistColumn), musicCursor.getString(durationColumn), musicCursor.getString(dataColumn)));


            } 
            while (musicCursor.moveToNext());
        }

这是播放列表中的一个:

public void getPlayists() {
    playlists = new ArrayList<Playlist>();
    // query external audio
    String[] projection = {
            android.provider.MediaStore.Audio.Playlists._ID,
            android.provider.MediaStore.Audio.Playlists.NAME };

    String[] projectionPl = { 
            android.provider.MediaStore.Audio.Media._ID,
            android.provider.MediaStore.Audio.Media.TITLE };

    ContentResolver resolver = context.getContentResolver();

    Uri playlistsUri = android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    Cursor playlistsCursor = resolver.query(playlistsUri, projection, null,
            null, null);
    // iterate over results if valid
    if (playlistsCursor != null && playlistsCursor.moveToFirst()) {
        // get columns
        int idColumn = playlistsCursor
                .getColumnIndex(android.provider.MediaStore.Audio.Playlists._ID);
        int nameColumn = playlistsCursor
                .getColumnIndex(android.provider.MediaStore.Audio.Playlists.NAME);
        // add songs to list

        do {
            long id = playlistsCursor.getLong(idColumn);

            Playlist pl = new Playlist(
                    playlistsCursor.getString(nameColumn), id);

            Cursor plCursor = resolver.query(
                    android.provider.MediaStore.Audio.Playlists.Members
                            .getContentUri("external", id), projectionPl,
                    null, null, null);

            if (plCursor != null && plCursor.moveToFirst()) {
                // get columns
                int idColumna = plCursor
                        .getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
                int nameColumna = plCursor
                        .getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
                // add songs to list
                do {
                    long ida = plCursor.getLong(idColumna);

                    String title = plCursor.getString(nameColumna);

                    for (Song song : songs) {

                        if (song.id == ida) {
                            pl.addSong(song);
                        }
                    }

                } while (plCursor.moveToNext());
            }
            playlists.add(pl);

        } while (playlistsCursor.moveToNext());
    }

有人可以准确解释歌曲ID是如何工作的,或者我如何修改我的方法以使它们有效?感谢。

1 个答案:

答案 0 :(得分:2)

你的播放列表库是针对android.provider.MediaStore.Audio.Playlists.Members而你接着从playlistsCursor获取值但是针对android.provider.MediaStore.Audio.Media 播放列表的返回结构是:

        values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, playorder);
        values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);

所以,如果您正在寻找音频ID,请更改

    int idColumn = playlistsCursor
            .getColumnIndex(android.provider.MediaStore.Audio.Playlists._ID);

    int idColumn = playlistsCursor
            .getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID);

ps:我之前发布过代码来创建播放列表等。 在Google Play或Amazon App Store上查看我的应用播放列表管理器以获取更多功能