加载 Android 中的封面音频艺术。

huangapple go评论61阅读模式
英文:

load cover audio art in android

问题

我想在Android中加载封面音频艺术以显示设备上的音频列表。为此,我使用了以下链接:

如下图所示:

加载 Android 中的封面音频艺术。

以及我的加载音频文件代码:

  private void loadAudio() {
        ContentResolver contentResolver = getContentResolver();

        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
        String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
        Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
        
        if (cursor != null && cursor.getCount() > 0) {
            audioList.clear();
            while (cursor.moveToNext()) {
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                Cursor cursor1 = contentResolver.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);

                Bitmap bitmap = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
                    try {
                        bitmap = contentResolver.loadThumbnail(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new Size(30, 30), null);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                String albumArt = "";
                if (cursor1 != null && cursor1.getCount() > 0)
                    while (cursor1.moveToNext()) {
                        albumArt = cursor.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                    }

                // 保存到audioList
                audioList.add(new Audio(data, title, album, artist, albumArt, bitmap));
            }
        }
        cursor.close();

        setAdapter();
    }

以及关于RecyclerView的MyAdapter

 @Override
    public void onBindViewHolder(@NonNull MainHolder holder, int position) {
        Audio audio = arrayList.get(position);
        holder.txtTitle.setText("标题:" + audio.getTitle());
        holder.txtAlbum.setText("专辑:" + audio.getAlbum());
        holder.txtArtist.setText("艺术家:" + audio.getArtist());
        File imgFile = new File(audio.getAlbumArt());
        Picasso.get().load(imgFile.getAbsolutePath()).into(holder.img);
        if (imgFile.exists()) {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            holder.img.setImageBitmap(myBitmap);
        }
    }

请帮助我。

英文:

I want to load cover audio art in android for show list of audio from device. For this purpose i used this following Link:

Like following picture:

加载 Android 中的封面音频艺术。

And my code for Load audio files:


private void loadAudio() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
audioList.clear();
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Cursor cursor1 = contentResolver.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);
Bitmap bitmap = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
try {
bitmap = contentResolver.loadThumbnail(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new Size(30, 30), null);
} catch (IOException e) {
e.printStackTrace();
}
}
String albumArt = "";
if (cursor1 != null && cursor1.getCount() > 0)
while (cursor1.moveToNext()) {
albumArt = cursor.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
}
// Save to audioList
audioList.add(new Audio(data, title, album, artist, albumArt, bitmap));
}
}
cursor.close();
setAdapter();
}

And in MyAdapter about recyclerview:

 @Override
public void onBindViewHolder(@NonNull MainHolder holder, int position) {
Audio audio = arrayList.get(position);
holder.txtTitle.setText("Title: " + audio.getTitle());
holder.txtAlbum.setText("Album: " + audio.getAlbum());
holder.txtArtist.setText("Artist: " + audio.getArtist());
File imgFile = new File(audio.getAlbumArt());
Picasso.get().load(imgFile.getAbsolutePath()).into(holder.img);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.img.setImageBitmap(myBitmap);
}
}

Help me please.

答案1

得分: 1

已解决:

  • 加载音频文件:
 private void loadAudio() {
        ContentResolver contentResolver = getContentResolver();

        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
        String sortOrder = MediaStore.Audio.Media.ARTIST + " DESC";
        Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);

        if (cursor != null && cursor.getCount() > 0) {
            audioList.clear();
            while (cursor.moveToNext()) {
                Audio audio = new Audio();
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                Bitmap thumbnail = null;
                String albumArt = null;
                Uri imageUri_t;

                Cursor cursor1 = contentResolver.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);

                while (cursor1.moveToNext()) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        Size thumbSize = new Size(100, 100);
                        try {
                            int thumbColumn = cursor1.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
                            int _thumpId = cursor1.getInt(thumbColumn);
                            imageUri_t = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, _thumpId);
                            thumbnail = getContentResolver().loadThumbnail(imageUri_t, thumbSize, null);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } else {
                        albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                    }
                }


                audio.setData(data);
                audio.setTitle(title);
                audio.setAlbum(album);
                audio.setArtist(artist);
                audio.setAlbumId(albumId);
                audio.setAlbumArt(albumArt);
                audio.setThumbnail(thumbnail);
                audioList.add(audio);
            }
        }
        cursor.close();

        setAdapter();
    }
  • 还有 MyAdapter 的一部分:
@Override
    public void onBindViewHolder(@NonNull MainHolder holder, int position) {
        Audio audio = arrayList.get(position);
        holder.txtTitle.setText("Title: " + audio.getTitle());
        holder.txtAlbum.setText("Album: " + audio.getAlbum());
        holder.txtArtist.setText("Artist: " + audio.getArtist());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (audio.getThumbnail() != null)
            holder.img.setImageBitmap(audio.getThumbnail());
        } else {
            if (audio.getAlbumArt() != null) {
                File imgFile = new File(audio.getAlbumArt());
                if (imgFile.exists()) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                    holder.img.setImageBitmap(myBitmap);
                }
            }
        }
    }

首次查询 MediaStore.Audio.Media.ARTIST 以获取来自 MediaStore 的音频信息。

在 Android 10(Q)及以下版本中,获取音频文件的缩略图存在差异。
因此,针对低于 Android 10 的查询如下所示:

String albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

您可以从 MediaStore.Audio.Albums 中的 ALBUM_ART 列中获取专辑封面。

然后,对于 Android 10,请按照以下方式进行查询:

MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI 上查询,其中 albumId 等于 MediaStore.Audio.Albums 中的 _ID 列。
然后通过图像 URI 从 Content Resolver 中加载缩略图...

希望这对您有用 加载 Android 中的封面音频艺术。

英文:

Solved:

  • Load audio file:
 private void loadAudio() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.ARTIST + " DESC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
audioList.clear();
while (cursor.moveToNext()) {
Audio audio = new Audio();
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Bitmap thumbnail = null;
String albumArt = null;
Uri imageUri_t;
Cursor cursor1 = contentResolver.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);
while (cursor1.moveToNext()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Size thumbSize = new Size(100, 100);
try {
int thumbColumn = cursor1.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
int _thumpId = cursor1.getInt(thumbColumn);
imageUri_t = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, _thumpId);
thumbnail = getContentResolver().loadThumbnail(imageUri_t, thumbSize, null);
} catch (IOException e) {
e.printStackTrace();
}
} else {
albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
}
}
audio.setData(data);
audio.setTitle(title);
audio.setAlbum(album);
audio.setArtist(artist);
audio.setAlbumId(albumId);
audio.setAlbumArt(albumArt);
audio.setThumbnail(thumbnail);
audioList.add(audio);
}
}
cursor.close();
setAdapter();
}
  • And a part of MyAdapter:
@Override
public void onBindViewHolder(@NonNull MainHolder holder, int position) {
Audio audio = arrayList.get(position);
holder.txtTitle.setText("Title: " + audio.getTitle());
holder.txtAlbum.setText("Album: " + audio.getAlbum());
holder.txtArtist.setText("Artist: " + audio.getArtist());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (audio.getThumbnail() != null)
holder.img.setImageBitmap(audio.getThumbnail());
} else {
if (audio.getAlbumArt() != null) {
File imgFile = new File(audio.getAlbumArt());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.img.setImageBitmap(myBitmap);
}
}
}
}

First query on MediaStore.Audio.Media.ARTIST about fetch audio info from MediaStore.

There is a difference About fetch thumbnail of Audio file in android 10 (Q) and below.
So query for lower than Android 10 is like follow:

String albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

You can get album art from ALBUM_ART column in MediaStore.Audio.Albums.

Then for Android 10 like follow this:

Query on MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI where albumId equals _ID column from MediaStore.Audio.Albums.
And load thumbnail from Content Resolver by image uri...

I hope it is useful 加载 Android 中的封面音频艺术。

huangapple
  • 本文由 发表于 2020年9月5日 20:35:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63754011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定