Mp3音乐文件列表实现

前一篇博文读取SD卡中所有MP3文件中有说明怎么获取所有Mp3文件的文件名,那是用遍历的方法实现的,但是Android有提供方法去获取音频文件,然后抓取Mp3文件就行了,最后把Mp3文件的一些属性也可以显示出来,我暂时显示了专辑图片(如果有则显示,没有的话显示默认的图片)、音乐名、歌手名,效果图如下:

 

1.资源管理器预览,主要是3个类文件和2个布局文件:

 

2.然后就是代码了

①先是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10sp" >
    </ListView>

</RelativeLayout>
activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:orientation="vertical" android:gravity="center" >

        <ImageView
            android:id="@+id/listImage"
            android:layout_width="40sp"
            android:layout_height="40sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50sp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/listName"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/list_Singer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:textSize="13sp" />
    </LinearLayout>

</LinearLayout>
musiclist.xml

 

②然后是音乐文件的属性类

package com.yfoliage.getmusiclistmore;

/**
 * @author YFoliage 歌曲信息类
 */
public class MusicInfo {
    private int musicIndex;  //排序号
    private int musicAlubmId;//专辑ID
    private String musicName;// 歌曲名
    private String musicSinger;// 歌手名
    private int musicTime;// 歌曲时间长度
    private String musicAlubm;// 专辑名称
    private int musicSize;// 曲歌大小
    private String musicPath;// 歌曲路径

    public int getMusicIndex() {
        return musicIndex;
    }

    public void setMusicIndex(int musicIndex) {
        this.musicIndex = musicIndex;
    }

    public int getMusicId() {
        return musicAlubmId;
    }

    public void setMusicId(int musicAlubmId) {
        this.musicAlubmId = musicAlubmId;
    }

    public String getMusicName() {
        return musicName;
    }

    public void setMusicName(String musicName) {
        this.musicName = musicName;
    }

    public String getMusicSinger() {
        return musicSinger;
    }

    public void setMusicSinger(String musicSinger) {
        this.musicSinger = musicSinger;
    }

    public int getMusicTime() {
        return musicTime;
    }

    public void setMusicTime(int musicTime) {
        this.musicTime = musicTime;
    }

    public String getMusicAlubm() {
        return musicAlubm;
    }

    public void setMusicAlubm(String musicAlubm) {
        this.musicAlubm = musicAlubm;
    }

    public int getMusicSize() {
        return musicSize;
    }

    public void setMusicSize(int musicSize) {
        this.musicSize = musicSize;
    }

    public String getMusicPath() {
        return musicPath;
    }

    public void setMusicPath(String musicPath) {
        this.musicPath = musicPath;
    }

}
MusicInfo

 

③重写的适配器类

package com.yfoliage.getmusiclistmore;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MusicAdapter extends BaseAdapter {
    // 用来获得ContentProvider(共享数据库)
    public ContentResolver cr;
    // 用来装查询到的音乐文件数据
    public Cursor cur;
    // 歌曲信息列表
    public List<MusicInfo> musicList;
    public Context context;

    public MusicAdapter(Context context) {
        this.context = context;
        // 取得数据库对象
        cr = context.getContentResolver();
        musicList = new ArrayList<MusicInfo>();

        String[] mString = new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.SIZE,
                MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA };
        // 查询所有音乐信息
        cur = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mString,
                null, null, null);

        if (cur != null) {
            // 移动游标到第一个
            cur.moveToFirst();
            int j = 1;
            for (int i = 0; i < cur.getCount(); i++) {
                if (cur.getString(0).endsWith(".mp3")) {// 过滤获取MP3文件
                    MusicInfo mInfo = new MusicInfo();
                    String musicName = cur.getString(0).substring(0,
                            cur.getString(0).lastIndexOf(".mp3"));
                    mInfo.setMusicIndex(j++);
                    mInfo.setMusicName(musicName);
                    mInfo.setMusicAlubm(cur.getString(1));
                    mInfo.setMusicSinger(cur.getString(2));
                    mInfo.setMusicTime(cur.getInt(3));
                    mInfo.setMusicSize(cur.getInt(4));
                    mInfo.setMusicId(cur.getInt(5));
                    mInfo.setMusicPath(cur.getString(6));
                    musicList.add(mInfo);
                }
                cur.moveToNext();
            }

        }
    }

    @Override
    public int getCount() {
        return musicList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return musicList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        LayoutInflater mlistLayout = LayoutInflater.from(context);
        View mlistView = mlistLayout.inflate(R.layout.musiclist, null);
        TextView songName = (TextView) mlistView.findViewById(R.id.listName);
        TextView singerName = (TextView) mlistView
                .findViewById(R.id.list_Singer);
        ImageView songImage = (ImageView) mlistView
                .findViewById(R.id.listImage);
        int mid = musicList.get(arg0).getMusicIndex();
        String musicName = musicList.get(arg0).getMusicName();
        String musciSinger = musicList.get(arg0).getMusicSinger();
        if (musciSinger.contains("<unknown>")) {
            musciSinger = "<未知>";
        }
        Bitmap img = getAlbumArt(musicList.get(arg0).getMusicId());
        songName.setText(mid + ". " + musicName);
        singerName.setText(musciSinger);
        songImage.setImageBitmap(img);
        return mlistView;
    }

    /**
     * 歌曲专辑图片显示,如果有歌曲图片,才会返回,否则为返回默认的图片
     * 
     * @param albumId
     *            专辑地址
     * @return 返回类型Bitmap
     */
    public Bitmap getAlbumArt(int albumId) {// albumId是音乐专辑的id
        Bitmap bitmap = null;
        Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);

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

        } catch (FileNotFoundException exception) {
            exception.printStackTrace();
            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.album);
        } catch (IOException e) {

            e.printStackTrace();
        }

        return bitmap;
    }
}
MusicAdapter

 

④最后是Activity

package com.yfoliage.getmusiclistmore;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    public MusicAdapter mAdapter;
    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.listView);
        mAdapter = new MusicAdapter(this);
        mListView.setAdapter(mAdapter);
    }


}
MainActivity

 

源代码下载

 

 

posted @ 2013-11-06 04:01  YFoliage  阅读(1091)  评论(0编辑  收藏  举报