我正在android上构建一个音乐播放器应用程序,但在快速加载设备上的歌曲列表时遇到问题。它在大约6.8-7秒内加载577首歌曲,这完全是太长了。有什么建议吗?
我想得到以下信息:
歌曲
歌曲名称
歌曲艺术家
唱片集名称
歌曲ID
无论是铃声还是通知(忽略它)
以下是我当前的算法:

public static ArrayList<Song> getSongList(Activity activity, String artistBound, Album albumBound) {
    long start = System.currentTimeMillis();
    ArrayList<Song> songList = new ArrayList<>();
    ContentResolver musicResolver = activity.getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

    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 albumIdColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.Media.ALBUM_ID);
        int ringColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.IS_RINGTONE);
        int notifColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.IS_NOTIFICATION);
        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            Artist thisArtist = new Artist(musicCursor.getString(artistColumn));
            if(artistBound != null && !thisArtist.getName().equals(artistBound)) continue;

            long albumId = musicCursor.getLong(albumIdColumn);
            Cursor albumCursor = activity.getContentResolver().query(
                    MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Albums.ALBUM},
                    MediaStore.Audio.Albums._ID + " = ?",
                    new String[]{Long.toString(albumId)},
                    null
            );
            boolean queryResult = albumCursor.moveToFirst();
            String albumCover = null;
            String albumName = null;
            Album album = null;
            if (queryResult) {
                albumCover = albumCursor.getString(0);
                albumName = albumCursor.getString(1);
                album = new Album(albumName, albumCover);
            }
            albumCursor.close();

            if(musicCursor.getInt(ringColumn) > 0 || musicCursor.getInt(notifColumn) > 0) {

            } else {
                if(albumBound == null || albumBound.getName().equals(album.getName())) {
                    songList.add(new Song(thisId, thisTitle, thisArtist, album));
                }
            }
        }
        while (musicCursor.moveToNext());
    }
    Collections.sort(songList);

    Log.d("app", "Got " + songList.size() + " songs in: " + (System.currentTimeMillis() - start) + "ms");
    return songList;
}

问题是,我试图在开始时加载这个列表,因为音乐播放器的第一个屏幕是歌曲列表。但就目前的情况来看,该应用程序在尝试加载时挂在这个屏幕上。
编辑:删除搜索相册信息的代码会使它运行得非常快如何优化唱片集信息搜索?

最佳答案

解决方案之一是使用延迟加载方法。其思想是不加载所有条目,而是加载一半数据集在您的情况下,这将大大减少加载时间在Internet上,您可以找到几个从服务器获取图像并显示它们的延迟加载实现。您可以用从本地存储器读取歌曲的方式编辑它检查接受的答案here

10-06 05:34
查看更多