android - 缓慢的音乐加载算法

标签 android algorithm performance mediastore audio-player android-music-player

我正在 Android 上构建一个音乐播放器应用程序,但我无法快速在设备上加载歌曲列表。它在大约 6.8-7 秒内加载 577 首歌曲,这实在是太长了。有什么建议吗?

我正在尝试获取以下信息: 歌曲

  • 歌曲名称
  • 歌曲艺术家
  • 专辑名
  • 歌曲编号
  • 是铃声还是通知(忽略)

这是我目前的算法:

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 .

关于android - 缓慢的音乐加载算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38956337/

相关文章:

android - 在显示 Activity 之前,如何在android中使用来自广播接收器的数据

android - 如何连接eclipse和GTV设备

android - 我需要填写什么数据安全表?

python - 如何在 Python 中执行二分法

PHP switch 语句 VS if elseif 语句基准

c++ - std::thread 的创建使主程序速度减慢 50%

java - 将列表中最喜欢的项目存储到 android 中的 SharedPreferences 中?

algorithm - 树 : Find node whose common path is shortest in relation to other nodes

java - 在图中构建随机哈密顿路径

java - 实时 Java 规范