获取歌曲图像非常慢(MediaMetadataRetriever)
Getting song image very slow (MediaMetadataRetriever)
我正在开发媒体播放器应用程序,我正在使用 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);
}
这个方法returns 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;
}
希望对您有所帮助。
我正在开发媒体播放器应用程序,我正在使用 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);
}
这个方法returns 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;
}
希望对您有所帮助。