简体   繁体   English

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

[英]Android App Development(Music Player)

I am working on a project of media player where i have fetched all the songs from the sd card as shown in below picture . 我正在一个媒体播放器项目中,已从SD卡中取出了所有歌曲,如下图所示。 See my app layout image here.. enter image description here 在此处查看我的应用布局图片。在此处输入图片说明

but now i want to add their corresponding song album image at the image view on the list view where the green android image is .... Like this I want to create it........ See here how i want to create list with pictures enter image description here 但现在我想在绿色Android图像所在的列表视图的图像视图上添加其对应的歌曲专辑图像。像这样,我要创建它........请参阅此处,我要用图片创建列表在这里输入图片说明

So please tell me how to get the album art of the each song and set them to the listview infront of their names......... 所以,请告诉我如何获取每首歌曲的专辑封面,并将它们设置在其名称前面的列表视图中.........

My Codes to fetch the songs are as follows.......... 我的用于提取歌曲的代码如下..........

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

Few years ago, I developed a music app too and this is the function where I retrieve the album list. 几年前,我也开发了一个音乐应用程序,这是我检索专辑列表的功能。
NB : its been almost 4 years now and I did not change my code so it may won't fit with new Android versions 注意:现在已经快四年了,我没有更改代码,因此可能不适合新的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());
    }
}

And the Album class 和专辑类

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

Hope it could help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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