简体   繁体   中英

How to show album art , song name ,duration, and the artist name in a ListView

hello i wanna make an android music app for that i want my listView which shows all the songs to display album art of that song , artist name , duration and song name

i have succeeded showing all the songs in the listview but unable to display album art etc

so can anyone help me in this??

Thanks in advance

You can use content provider for this.

Hope this code may help you to start up.

final Cursor mCursor = getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
                        MediaStore.Audio.Media.DATA,
                        MediaStore.Audio.Media.ARTIST,
                        MediaStore.Audio.Media.ALBUM_ID }, null, null,
                "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

        int count = mCursor.getCount();

        String[] songs = new String[count];

        if (mCursor.moveToFirst()) {
            do {
                String songname = mCursor
                        .getString(mCursor
                                .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                String sonpath = mCursor.getString(mCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                String artistname = mCursor.getString(mCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                String albumid = mCursor
                        .getString(mCursor
                                .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
    } while (mCursor.moveToNext());
            Constant.sdCardmusic = allsongs;
        }

        mCursor.close();
 }

If you want to get album picture then you can pass album id getting from above code into below method:

private Bitmap getArtistImage(String albumid) {
        Bitmap artwork = null;
        try {
            Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");
            Uri uri = ContentUris.withAppendedId(sArtworkUri,
                    Long.valueOf(albumid));
            ContentResolver res = mContext.getContentResolver();
            InputStream in = res.openInputStream(uri);
            artwork = BitmapFactory.decodeStream(in);

        } catch (Exception e) {
            Log.e("Exception", e.toString());
        }
        return artwork;
    }

Google在Github上提供了用于电话,chromecast和AUTO的音乐播放器的绝佳示例, 网址为https://github.com/googlesamples/android-UniversalMusicPlayer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM