【问题标题】:how to get thumbnail from audio url in android using glide如何使用 glide 从 android 中的音频 url 获取缩略图
【发布时间】:2022-01-01 14:33:42
【问题描述】:

尝试使用 glide 从 url 加载音频缩略图但不工作

Glide
                .with(this)
                .load("http://api.boleiachain.com/upload/musictest.mp3")
                .apply(options)
                .centerCrop()
                .placeholder(R.drawable.ic_loading)
                .into(civ_image);

我已经试过了

public  Bitmap coverpicture(String path) {

        Bitmap bitmap=null;
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(path, new HashMap<String, String>());
        try {
            final byte[] coverImage = mediaMetadataRetriever.getEmbeddedPicture();
             bitmap= BitmapFactory.decodeByteArray(coverImage, 0, coverImage.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;

    }

Glide
                .with(this)
                .load(coverpicture("http://api.boleiachain.com/upload/musictest.mp3"))
                .apply(options)
                .centerCrop()
                .placeholder(R.drawable.ic_loading)
                .into(civ_image);

它可以工作但有延迟,这在 recyclerView 适配器中不是一个好习惯

【问题讨论】:

  • 无论如何,网络通话都需要时间,

标签: android url audio


【解决方案1】:

您可以在调用coverPicture方法之前缓存位图并检查缓存是否存在

  ....load(getCoverpicture("http://api.boleiachain.com/upload/musictest.mp3"))



HashMap<String,Bitmap> cache = new HashMap<String,Bitmap>();
public  Bitmap getCoverpicture(String path) {
    if(cache.containsKey(path)){ return cache.get(path);
    }else{
        Bitmap bitmap = coverpicture(path);
        cache.put(path,bitmap);
        return bitmap;
    }
}

它有一些语法错误

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2011-05-25
    • 2019-12-19
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多