【问题标题】:Getting song image very slow (MediaMetadataRetriever)获取歌曲图像非常慢(MediaMetadataRetriever)
【发布时间】:2018-01-08 12:59:11
【问题描述】:

我正在开发媒体播放器应用程序,我正在使用 MediaMetadataRetriever 获取歌曲图像,我正在获取图像,并且我使用 Glide 进行设置,但图像需要大约 7-9 秒加载非常非常慢。我也尝试使用 BitmapFactory,但也是在同一时间。

所以有什么更快的方法可以让我获得歌曲图像。

提前致谢

这是我使用 MediaMetadataRetriever 获取图像的代码。

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(songpath);
    byte[] art = retriever.getEmbeddedPicture();

    if (art != null) {

        Glide.with(c).load(art)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(holder.songimage);

        //holder.songimage.setImageBitmap(BitmapFactory.decodeByteArray(art, 0, art.length));
    } else {
        Glide.with(c).load(R.drawable.splash)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(holder.songimage);

        //holder.songimage.setImageResource(R.drawable.splash);
    }

【问题讨论】:

  • 顺便可以使用content provider来获取歌曲图片。我目前正在使用它的工作良好,尝试使用 GlideApp 可能会更快。
  • 如何!!!您可以详细说明或分享代码@kdblue

标签: android android-layout android-fragments media-player


【解决方案1】:

此方法返回ArrayList<CommonModel>

public static ArrayList<CommonModel> getAllMusicPathList(Context context,String selectAll) {
        ArrayList<CommonModel> musicPathArrList = new ArrayList<>();
        Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor cursorAudio = context.getContentResolver().query(songUri, null, null, null, null);
        if (cursorAudio != null && cursorAudio.moveToFirst()) {

            Cursor cursorAlbum;
            if (cursorAudio != null && cursorAudio.moveToFirst()) {

                do {
                    Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
                    cursorAlbum = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                            new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                            MediaStore.Audio.Albums._ID + "=" + albumId, null, null);

                        if(cursorAlbum != null && cursorAlbum.moveToFirst()){
                            String albumCoverPath = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                            String data = cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.DATA));

                            if("selectAll".equals(selectAll))
                            {
                                musicPathArrList.add(new CommonModel(data,albumCoverPath, true));
                            }
                            else
                            {
                                musicPathArrList.add(new CommonModel(data,albumCoverPath, false));
                            }
                        }

                } while (cursorAudio.moveToNext());
            }
        }
        return musicPathArrList;
    }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 2016-05-28
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多