Mp3音樂文件列表實現

前一篇博文讀取SD卡中全部MP3文件中有說明怎麼獲取全部Mp3文件的文件名,那是用遍歷的方法實現的,可是Android有提供方法去獲取音頻文件,而後抓取Mp3文件就好了,最後把Mp3文件的一些屬性也能夠顯示出來,我暫時顯示了專輯圖片(若是有則顯示,沒有的話顯示默認的圖片)、音樂名、歌手名,效果圖以下:html

 

1.資源管理器預覽,主要是3個類文件和2個佈局文件:java

 

2.而後就是代碼了android

①先是佈局文件數據庫

<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

 

②而後是音樂文件的屬性類app

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

 

③重寫的適配器類ide

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

 

源代碼下載post

相關文章
相關標籤/搜索