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

倾然丶 夕夏残阳落幕 提交于 2019-12-08 03:51:28

问题


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


回答1:


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;
    }



回答2:


Google has a great example of a music player for phone , chromecast and AUTO on Github https://github.com/googlesamples/android-UniversalMusicPlayer



来源:https://stackoverflow.com/questions/30004653/how-to-show-album-art-song-name-duration-and-the-artist-name-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!