小编典典

如何从歌曲中排序专辑?

java

所以我的问题是,当我尝试对专辑进行排序时,专辑标题和专辑封面是错误的。

我尝试对专辑ID进行排序,但这并不能解决问题,因为专辑ID显然与艺术排序无关。

当我忽略排序时,一切都是正确的,但是当我尝试对它们进行排序时,专辑名称与专辑封面不匹配。

如何将片段中的专辑排序?

在这里您可以找到我的代码。

提前致谢,

文斯

歌曲模型

 // Columns I'll retrieve from the song table
    String[] columns = {
            SONG_ID,
            SONG_TITLE,
            SONG_ARTIST,
            SONG_ALBUM,
            SONG_ALBUMID,
            SONG_FILEPATH,
    };

    // Limits results to only show music files.
    //
    // It's a SQL "WHERE" clause - it becomes `WHERE IS_MUSIC=1`.
    //
    final String musicsOnly = MediaStore.Audio.Media.IS_MUSIC + "=1";

    // Querying the system
    cursor = resolver.query(musicUri, columns, musicsOnly, null, null);

    if (cursor != null && cursor.moveToFirst())
    {

        do {
            // Creating a song from the values on the row
            Song song = new Song(cursor.getInt(cursor.getColumnIndex(SONG_ID)),
                    cursor.getString(cursor.getColumnIndex(SONG_FILEPATH)));

            song.setTitle      (cursor.getString(cursor.getColumnIndex(SONG_TITLE)));
            song.setArtist     (cursor.getString(cursor.getColumnIndex(SONG_ARTIST)));
            song.setAlbumID    (cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID)));
            song.setAlbum      (cursor.getString(cursor.getColumnIndexOrThrow(SONG_ALBUM)));
            // Using the previously created genre and album maps
            // to fill the current song genre.
            String currentGenreID   = songIdToGenreIdMap.get(Long.toString(song.getId()));
            String currentGenreName = genreIdToGenreNameMap.get(currentGenreID);
            song.setGenre(currentGenreName);

            // Adding the song to the global list
            songs.add(song);
        }
        while (cursor.moveToNext());
    }
    else
    {
        // What do I do if I can't find any songs?

    }
    cursor.close();

public ArrayList<String> getArtists() {

    ArrayList<String> artists = new ArrayList<String>();

    for (Song song : songs) {
        String artist = song.getArtist();

        if ((artist != null) && (! artists.contains(artist)))
            artists.add(artist);
    }

    // Making them alphabetically sorted
    Collections.sort(artists, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    });
    return artists;
}

/**
 * Returns an alphabetically sorted list with all the
 * albums of the scanned songs.
 *
 * @note This method might take a while depending on how
 *       many songs you have.
 */

public ArrayList<String> getAlbums() {

    ArrayList<String> albums = new ArrayList<String>();

    for (Song song : songs) {
        String album = song.getAlbum();

        if ((album != null) && (! albums.contains(album)))
            albums.add(album);

    }

歌曲班

public class Song implements Serializable {

private long id;
private String data;
private String title = "";
private String artist = "";
private int   albumid      = -1;
private String album = "";
private String genre = "";


public Song(long songId, String songData){
    this.id = songId;
    this.data = songData;
}

public long getId(){
    return id;
}
public String getData(){return data;}

//Optional meta data

public void setTitle(String title){
    this.title = title;
}
public String getTitle() {
    return title;
}

public void setArtist(String artist){
    this.artist = artist;
}
public String getArtist() {
    return artist;
}

public int getAlbumID() {
    return albumid;
}
public void setAlbumID(int albumid) { this.albumid = albumid; }

public void  setAlbum(String album){
    this.album = album;
}
public String getAlbum() { return album; }

public void setGenre(String genre) {
    this.genre = genre;
}
public String getGenre() {
    return genre;
}


}

阅读 503

收藏
2020-11-30

共1个答案

小编典典

首先,我不确定在存储按歌曲存储的返回值时为什么要尝试按专辑排序(请参见上面的@Usman Rafi),但是。

将全局数组列表添加到片段的顶部

ArrayList<Song> Albums = new Arraylist<>();

不要尝试添加流派信息-您不需要它来达到目的

我尝试对专辑ID进行排序,但这并不能解决问题,因为专辑ID显然与艺术排序无关。

专辑封面Uri可以写成:

ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),
    cursor.getInt(cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID))));

因此,专辑封面和album_id实际上是密不可分的。

所以我的问题是当我尝试对专辑进行排序时…

在查询的选择变量中使用MediaStore.Audio.Media.IS_MUSIC +“ = 1)GROUP BY(” +
MediaStore.Audio.Media.ALBUM …

这将返回唯一的专辑名称(也将仅返回专辑中的一首歌曲),如果在您的媒体存储数据库中重复该专辑(通过从同一专辑中获得几首歌曲)重复该专辑,则只会添加与您的查询匹配的第一个实例到您的光标。

对专辑进行排序…

使用排序顺序对相册返回的光标行进行排序;我个人使用sql的字母顺序对其进行排序(符号,数字,a,b,c…。)

您应在此处注意,排序是区分大小写的,除非您指定“ COLLATE NOCASE”

编写查询并对其进行排序,我将使用以下代码:

String[] projection = {"DISTINCT " + MediaStore.Audio.Media.ALBUM_ID,
                         MediaStore.Audio.Media._ID, 
                         MediaStore.Audio.Media.TITLE, 
                         MediaStore.Audio.Media.ARTIST, 
                         MediaStore.Audio.Media.DATA, 
                         MediaStore.Audio.Media.ALBUM,
                         MediaStore.Audio.Media.IS_MUSIC};

String selection = MediaStore.Audio.Media.IS_MUSIC + 
                   "=1 ) GROUP BY (" + MediaStore.Audio.Media.ALBUM_ID;

String sort = MediaStore.Audio.Media.ALBUM + " COLLATE NOCASE ASC";

Cursor cursor = context.
                 getContentResolver().
                 query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
                 projection,
                 selection,
                 null,
                 sort);

之后,您可以简单地在光标中移动,将每一行添加到所构建的数据对象中,无需进一步排序,并且事物应该以正确的顺序进行。

我个人只是遍历

if(cursor != null && cursor.getCount >0){
    while(cursor.moveToNext()){
        //create new song item and add the album information you need
        Song album = new Song(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
           cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));

            album.setAlbumId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
            album.setAlbumId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));

        //add the Song item to the global arraylist
        Albums.add(album)
    }
}

cursor.close();

您现在可以按数组列表中的位置访问排序的专辑信息…您可以使用我在顶部显示的Uri构建器了解专辑封面…

像这样

Song Album = Albums.get(position);     
imageView.setURI(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),
    Album.getAlbumID());

我希望这对您有用。

我仍然认为您应该建立一个名为Album的数据类

2020-11-30