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

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

load cover audio art in android

问题

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

如下图所示:

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

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

  1. private void loadAudio() {
  2. ContentResolver contentResolver = getContentResolver();
  3. Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  4. String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
  5. String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
  6. Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
  7. if (cursor != null && cursor.getCount() > 0) {
  8. audioList.clear();
  9. while (cursor.moveToNext()) {
  10. String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
  11. String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  12. String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  13. String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  14. int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
  15. Cursor cursor1 = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
  16. new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
  17. MediaStore.Audio.Albums._ID + "=" + albumId, null, null);
  18. Bitmap bitmap = null;
  19. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  20. try {
  21. bitmap = contentResolver.loadThumbnail(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new Size(30, 30), null);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. String albumArt = "";
  27. if (cursor1 != null && cursor1.getCount() > 0)
  28. while (cursor1.moveToNext()) {
  29. albumArt = cursor.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
  30. }
  31. // 保存到audioList
  32. audioList.add(new Audio(data, title, album, artist, albumArt, bitmap));
  33. }
  34. }
  35. cursor.close();
  36. setAdapter();
  37. }

以及关于RecyclerView的MyAdapter

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

请帮助我。

英文:

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:

  1. private void loadAudio() {
  2. ContentResolver contentResolver = getContentResolver();
  3. Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  4. String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
  5. String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
  6. Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
  7. if (cursor != null && cursor.getCount() > 0) {
  8. audioList.clear();
  9. while (cursor.moveToNext()) {
  10. String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
  11. String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  12. String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  13. String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  14. int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
  15. Cursor cursor1 = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
  16. new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
  17. MediaStore.Audio.Albums._ID + "=" + albumId, null, null);
  18. Bitmap bitmap = null;
  19. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
  20. try {
  21. bitmap = contentResolver.loadThumbnail(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new Size(30, 30), null);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. String albumArt = "";
  27. if (cursor1 != null && cursor1.getCount() > 0)
  28. while (cursor1.moveToNext()) {
  29. albumArt = cursor.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
  30. }
  31. // Save to audioList
  32. audioList.add(new Audio(data, title, album, artist, albumArt, bitmap));
  33. }
  34. }
  35. cursor.close();
  36. setAdapter();
  37. }

And in MyAdapter about recyclerview:

  1. @Override
  2. public void onBindViewHolder(@NonNull MainHolder holder, int position) {
  3. Audio audio = arrayList.get(position);
  4. holder.txtTitle.setText("Title: " + audio.getTitle());
  5. holder.txtAlbum.setText("Album: " + audio.getAlbum());
  6. holder.txtArtist.setText("Artist: " + audio.getArtist());
  7. File imgFile = new File(audio.getAlbumArt());
  8. Picasso.get().load(imgFile.getAbsolutePath()).into(holder.img);
  9. if (imgFile.exists()) {
  10. Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
  11. holder.img.setImageBitmap(myBitmap);
  12. }
  13. }

Help me please.

答案1

得分: 1

已解决:

  • 加载音频文件:
  1. private void loadAudio() {
  2. ContentResolver contentResolver = getContentResolver();
  3. Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  4. String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
  5. String sortOrder = MediaStore.Audio.Media.ARTIST + " DESC";
  6. Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
  7. if (cursor != null && cursor.getCount() > 0) {
  8. audioList.clear();
  9. while (cursor.moveToNext()) {
  10. Audio audio = new Audio();
  11. String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
  12. String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  13. String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  14. String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  15. int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
  16. Bitmap thumbnail = null;
  17. String albumArt = null;
  18. Uri imageUri_t;
  19. Cursor cursor1 = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
  20. new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
  21. MediaStore.Audio.Albums._ID + "=" + albumId, null, null);
  22. while (cursor1.moveToNext()) {
  23. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  24. Size thumbSize = new Size(100, 100);
  25. try {
  26. int thumbColumn = cursor1.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
  27. int _thumpId = cursor1.getInt(thumbColumn);
  28. imageUri_t = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, _thumpId);
  29. thumbnail = getContentResolver().loadThumbnail(imageUri_t, thumbSize, null);
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. } else {
  34. albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
  35. }
  36. }
  37. audio.setData(data);
  38. audio.setTitle(title);
  39. audio.setAlbum(album);
  40. audio.setArtist(artist);
  41. audio.setAlbumId(albumId);
  42. audio.setAlbumArt(albumArt);
  43. audio.setThumbnail(thumbnail);
  44. audioList.add(audio);
  45. }
  46. }
  47. cursor.close();
  48. setAdapter();
  49. }
  • 还有 MyAdapter 的一部分:
  1. @Override
  2. public void onBindViewHolder(@NonNull MainHolder holder, int position) {
  3. Audio audio = arrayList.get(position);
  4. holder.txtTitle.setText("Title: " + audio.getTitle());
  5. holder.txtAlbum.setText("Album: " + audio.getAlbum());
  6. holder.txtArtist.setText("Artist: " + audio.getArtist());
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  8. if (audio.getThumbnail() != null)
  9. holder.img.setImageBitmap(audio.getThumbnail());
  10. } else {
  11. if (audio.getAlbumArt() != null) {
  12. File imgFile = new File(audio.getAlbumArt());
  13. if (imgFile.exists()) {
  14. Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
  15. holder.img.setImageBitmap(myBitmap);
  16. }
  17. }
  18. }
  19. }

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

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

  1. 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:
  1. private void loadAudio() {
  2. ContentResolver contentResolver = getContentResolver();
  3. Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  4. String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
  5. String sortOrder = MediaStore.Audio.Media.ARTIST + " DESC";
  6. Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
  7. if (cursor != null && cursor.getCount() > 0) {
  8. audioList.clear();
  9. while (cursor.moveToNext()) {
  10. Audio audio = new Audio();
  11. String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
  12. String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  13. String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  14. String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  15. int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
  16. Bitmap thumbnail = null;
  17. String albumArt = null;
  18. Uri imageUri_t;
  19. Cursor cursor1 = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
  20. new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
  21. MediaStore.Audio.Albums._ID + "=" + albumId, null, null);
  22. while (cursor1.moveToNext()) {
  23. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  24. Size thumbSize = new Size(100, 100);
  25. try {
  26. int thumbColumn = cursor1.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
  27. int _thumpId = cursor1.getInt(thumbColumn);
  28. imageUri_t = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, _thumpId);
  29. thumbnail = getContentResolver().loadThumbnail(imageUri_t, thumbSize, null);
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. } else {
  34. albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
  35. }
  36. }
  37. audio.setData(data);
  38. audio.setTitle(title);
  39. audio.setAlbum(album);
  40. audio.setArtist(artist);
  41. audio.setAlbumId(albumId);
  42. audio.setAlbumArt(albumArt);
  43. audio.setThumbnail(thumbnail);
  44. audioList.add(audio);
  45. }
  46. }
  47. cursor.close();
  48. setAdapter();
  49. }
  • And a part of MyAdapter:
  1. @Override
  2. public void onBindViewHolder(@NonNull MainHolder holder, int position) {
  3. Audio audio = arrayList.get(position);
  4. holder.txtTitle.setText("Title: " + audio.getTitle());
  5. holder.txtAlbum.setText("Album: " + audio.getAlbum());
  6. holder.txtArtist.setText("Artist: " + audio.getArtist());
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  8. if (audio.getThumbnail() != null)
  9. holder.img.setImageBitmap(audio.getThumbnail());
  10. } else {
  11. if (audio.getAlbumArt() != null) {
  12. File imgFile = new File(audio.getAlbumArt());
  13. if (imgFile.exists()) {
  14. Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
  15. holder.img.setImageBitmap(myBitmap);
  16. }
  17. }
  18. }
  19. }

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:

  1. 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:

确定