Android应用开发(音乐播放器)

时间:2017-01-31 15:09:14

标签: java android android-studio

我正在制作一个媒体播放器项目,我已经从SD卡中获取了所有歌曲,如下图所示。在这里查看我的应用布局图像.. enter image description here

但现在我想在列表视图的图像视图中添加相应的歌曲专辑图像,其中绿色的android图像是.... 像这样我想创建它........看到这里我想如何创建图片列表 enter image description here

所以请告诉我如何获得每首歌的专辑封面并将它们设置为名单前面的列表视图.........

我的获取歌曲的代码如下..........

public class SongList extends Activity{
    Cursor c;
    int index;
    int count;
    String audioFilePath;
    MediaPlayer mp;

    ArrayList songs;

    public  void onCreate(Bundle SIS){
        super.onCreate(SIS);
        setContentView(R.layout.song_list);

        String orderBy= MediaStore.Audio.Media.TITLE;
        String cols[]=     {MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME};
        c=this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,cols,null,null,orderBy);

    songs=new ArrayList();
    if(c!=null){
        while(c.moveToNext()){
            songs.add(c.getString(1).toString());
        }
    }
    ArrayAdapter adapter = new     ArrayAdapter(this,R.layout.custom_list_layout,R.id.listtext,songs);
    final ListView lv =(ListView)findViewById(R.id.songlist);
    lv.setAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

几年前,我开发了一个音乐应用程序,这是我检索专辑列表的功能 NB:它已经差不多4年了,我没有改变我的代码,所以它可能不适合新的Android版本

private void        retrieveAlbums(){
    try {
        String      selection = MediaStore.Audio.Albums._ID + " != 0";
        String[]    projection = {
                "DISTINCT " + MediaStore.Audio.Albums._ID,
                MediaStore.Audio.Albums.ALBUM_KEY,
                MediaStore.Audio.Albums.ALBUM, 
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.NUMBER_OF_SONGS
        };

        Cursor      cursor = context.getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                projection, selection, null, null);

        Album       tmp = null;

        if (cursor.moveToFirst()) {
            do {

                tmp = new Album(cursor.getInt(0),
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getInt(4));

                Uri sArtworkUri = Uri
                        .parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, tmp.getId());

                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(
                            context.getContentResolver(), albumArtUri);

                } catch (FileNotFoundException exception) {
                    exception.printStackTrace();
                    bitmap = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.ic_launcher);
                } catch (IOException e) {
                    e.printStackTrace();
                    bitmap = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.ic_launcher);
                    e.printStackTrace();
                }
                tmp.setArt(bitmap);
                albumList.add(tmp);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }  catch (SQLException e) {
        Log.e("Exception on query albums", e.toString());
    }
}

和专辑类

public class Album {
private int     id;
private String  key;
private String  name;
private String  artist;
private int     nbSong;
private Bitmap  art;
private Bitmap  banner;

public Album(int id, String key, String name, String artist, int nbSong) {
    super();
    this.id = id;
    this.key = key;
    this.name = name;
    this.artist = artist;
    this.nbSong = nbSong;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getNbSong() {
    return nbSong;
}
public void setNbSong(int nbSong) {
    this.nbSong = nbSong;
}
public String getArtist() {
    return artist;
}
public void setArtist(String artist) {
    this.artist = artist;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getKey() {
    return key;
}
public void setKey(String key) {
    this.key = key;
}
public Bitmap getArt() {
    return art;
}
public void setArt(Bitmap art) {
    this.art = art;
}
public Bitmap getBanner() {
    return banner;
}
public void setBanner(Bitmap banner) {
    this.banner = banner;
}
}

希望它可以帮到你