RecyclerView花费太多时间来加载歌曲

时间:2018-05-28 15:38:46

标签: android android-recyclerview android-asynctask mediastore audio-player

我正在尝试查询我的Android设备中的所有歌曲。它有大约900首歌曲。歌曲加载很好但是在recyclerView中显示需要花费很多时间。在recyclerView中显示所有歌曲大约需要9-10秒。代码如下。有没有其他方法可以实现这一点,那么可以减少加载时间吗?

代码:

 private void queryForSongsInTheBg(final Uri uri, final String selection, final String[] whereVal){

    @SuppressLint("StaticFieldLeak") AsyncTask<Void,Void,Void> songManager = new AsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            getView().findViewById(R.id.querySongsProgressBar).setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... voids) {

            String[] projection = {MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.ALBUM_ID,
                    MediaStore.Audio.Media.DATE_ADDED,
                    MediaStore.Audio.Media.YEAR};

            Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
                        String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                        String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                        String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                        Long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                        String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                        Long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                        String date_added = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED));
                        Integer year = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.YEAR));
                        String albumArtUri = String.valueOf(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumId));
                        checkAlbumArt = getCoverArtPath(albumId);
                        if (checkAlbumArt == null || checkAlbumArt.isEmpty())
                            albumArtUri = getAlbumArtFromInternet(artist, name);

                        SongInfoModel s = new SongInfoModel(id, name, artist, date_added, album, year, duration, data, albumId, albumArtUri);
                        SongList.add(s);


                    } while (cursor.moveToNext());
                }


                cursor.close();

            }






            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            getView().findViewById(R.id.querySongsProgressBar).setVisibility(View.GONE);
            songAdapter = new SongAdapter(getContext(), SongList, listener);
            recyclerView.setAdapter(songAdapter);
        }
    };

    songManager.execute();
}

0 个答案:

没有答案