如何在Android中为音乐播放器创建缩略图播放列表?

时间:2016-03-19 15:55:23

标签: android imageview thumbnails grid-layout audio-player android-music-player

我在android中开发了一个简单的媒体播放器应用程序。它有一个列表视图,可以加载SD卡中的所有歌曲,搜索栏和播放按钮。我需要为我的应用创建一个专辑封面图库(比如谷歌播放音乐应用)。我用这段代码从SD卡中获取所有歌曲,

public ArrayList<String> GetFiles(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File f = new File(DirectoryPath);
        f.mkdirs();
        File[] files = f.listFiles();
        if (files.length == 0) {
            return null;
        }else {
            for (int i=0; i<files.length; i++)
                MyFiles.add(files[i].getName());
        }

        return MyFiles;
    }
  1. 如何改进此代码以返回歌曲名称和专辑封面?

  2. 如何为每首歌创建缩略图视图?

     _________
    |         |
    | image   |
    |         |
    |_________|
    |  title  |
    |_________|
    

1 个答案:

答案 0 :(得分:0)

您需要使用内容解析器。

 Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            // Perform a query on the content resolver. The URI we're passing specifies that we
            // want to query for all audio media on external storage (e.g. SD card)
            Cursor cur = contentResolver.query(uri, null,
                    MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
            if (cur == null) {
                Log.e(TAG, "Failed to retrieve music: cursor is null :-(");
                subscriber.onError(new Throwable("Failed to retrieve music: cursor is null :-("));
                return;
            }
            if (!cur.moveToFirst()) {
                subscriber.onError(new Throwable("No results. :( Add some tracks!"));

                Log.e(TAG, "Failed to move cursor to first row (no query results).");
                return;
            }
            int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
            int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
            int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
            int dataColumn = cur.getColumnIndex(MediaStore.Audio.Media.DATA);
            ArrayList<LocalTrack> tracks = new ArrayList<>();

            do {
                Log.i(TAG, "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn));
                tracks.add(new LocalTrack(
                        cur.getLong(idColumn),
                        cur.getString(artistColumn),
                        cur.getString(titleColumn),
                        cur.getLong(albumColumn),
                        cur.getLong(durationColumn),
                        cur.getString(dataColumn)));
            } while (cur.moveToNext());
            Log.i(TAG, "Done querying media. MusicRetriever is ready.");
            cur.close();


            for (LocalTrack localTrack : tracks) {
                localTrack.setArtPath(findAlbumArt(localTrack));
            }
            subscriber.onNext(tracks);
public String findAlbumArt(LocalTrack localTrack) {
    Cursor cursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
            MediaStore.Audio.Albums._ID + "=?",
            new String[]{String.valueOf(localTrack.getAlbum())},
            null);

    String path = null;
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
            // do whatever you need to do
            cursor.close();
        }
    }

    return path;
}