Android音乐播放器(2)--从SD卡中读取音乐

首先提一下我的UI设计,对于主页面,用到了侧滑效果,这样在一个页面上可以有更多按钮而不显得拥挤,其实也就是我的另一篇博客中(http://blog.csdn.net/ye_yun_lin/article/details/23177987)提到的方法,而我自己没有重新写,直接拿过来用。下面是我三个Activity的布局设计:

home.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/repeat_bg"
        android:gravity="bottom|center_horizontal"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            android:paddingBottom="10dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_weight="1" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="25dp"
                    android:text="My Music"
                    android:textColor="@android:color/black"
                    android:textSize="35dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_weight="3"
                android:gravity="center_horizontal" >

                <ImageView
                    android:id="@+id/button1"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@drawable/home_chilun_item_press"
                    android:clickable="true" />
            </LinearLayout>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="25dp" >

            <ImageView
                android:id="@+id/listbutton"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/list_item_press" />

            <ImageView
                android:id="@+id/cloudbutton"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/listbutton"
                android:background="@drawable/cloud_item_press" />

            <ImageView
                android:id="@+id/lovebutton"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_below="@id/listbutton"
                android:layout_marginTop="10dp"
                android:background="@drawable/love_item_press" />

            <ImageView
                android:id="@+id/localbutton"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_alignLeft="@id/cloudbutton"
                android:layout_alignTop="@id/lovebutton"
                android:background="@drawable/local_item_press" />

            <ImageView
                android:id="@+id/cechebutton"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_below="@id/lovebutton"
                android:layout_marginTop="10dp"
                android:background="@drawable/cache_item_press" />

            <ImageView
                android:id="@+id/downloadbutton"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_alignLeft="@id/localbutton"
                android:layout_alignTop="@id/cechebutton"
                android:background="@drawable/dowmload_item_press" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="@drawable/mini_background"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:src="@drawable/pan" />

            <LinearLayout
                android:id="@+id/clickToPlaying"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:clickable="true"
                android:orientation="vertical"
                android:paddingLeft="5dp"
                android:paddingTop="12dp" >

                <TextView
                    android:id="@+id/music_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Music Map"
                    android:textColor="@android:color/white"
                    android:textSize="18dp" />

                <TextView
                    android:id="@+id/singer_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="音乐与你同在"
                    android:textColor="@android:color/white"
                    android:textSize="12dp" />
            </LinearLayout>

            <ImageView
                android:id="@+id/homepause"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:background="@drawable/home_button_item"
                android:clickable="true" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:background="@drawable/content_background"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/scanningtbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview1"
                style="@style/imageviewstyle"
                android:src="@drawable/music" />

            <TextView
                style="@style/textviewstyle"
                android:layout_toRightOf="@id/imageview1"
                android:text="扫描音乐" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/playingbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview2"
                style="@style/imageviewstyle"
                android:src="@drawable/musicplaying" />

            <TextView
                android:id="@+id/statustext"
                style="@style/textviewstyle"
                android:layout_toRightOf="@id/imageview2"
                android:text="顺序播放" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/sleepbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview3"
                style="@style/imageviewstyle"
                android:src="@drawable/sleep" />

            <TextView
                style="@style/textviewstyle"
                android:layout_toRightOf="@id/imageview3"
                android:text="睡眠模式" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/downbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview4"
                style="@style/imageviewstyle"
                android:src="@drawable/down" />

            <TextView
                style="@style/textviewstyle"
                android:layout_toRightOf="@id/imageview4"
                android:text="下载管理" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/soundbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview5"
                style="@style/imageviewstyle"
                android:src="@drawable/vivo" />

            <TextView
                style="@style/textviewstyle"
                android:layout_toRightOf="@id/imageview5"
                android:text="音效" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/managerbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview6"
                style="@style/imageviewstyle"
                android:src="@drawable/maneger" />

            <TextView
                style="@style/textviewstyle"
                android:layout_toRightOf="@id/imageview6"
                android:text="设置" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/aboutbutton"
            style="@style/Relativelayoutstyle" >

            <ImageView
                android:id="@+id/imageview7"
                style="@style/imageviewstyle"
                android:src="@drawable/man" />

            <TextView
                style="@style/textviewstyle"
                android:layout_width="wrap_content"
                android:layout_toRightOf="@id/imageview7"
                android:text="关于我们" />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

music_list.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/RelativeLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"
    android:background="#ecebd7">
    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@drawable/main_top_bar_bg">
            <ImageView
                android:id="@+id/backtohome"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:clickable="true"
                android:background="@drawable/activity_item_back"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="所有歌曲"
                android:textSize="25dp"
                android:layout_centerInParent="true"
                android:textColor="@android:color/black" />
        </RelativeLayout> 
    <ListView
		android:layout_below="@id/top" 
		android:padding="5dp"
        android:id="@android:id/list"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
    </ListView> 
</RelativeLayout> 

music_start.xml:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ecebd7" >

    <RelativeLayout
        android:id="@+id/playingtop"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@drawable/main_top_bar_bg" >

        <ImageView
            android:id="@+id/back"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:background="@drawable/activity_item_back"
            android:clickable="true" />

        <TextView
            android:id="@+id/music_start_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="歌曲"
            android:textColor="@android:color/black"
            android:textSize="20dp" />
    </RelativeLayout>

    <Button
        android:id="@+id/music_repeat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/playingtop"
        android:background="@drawable/repeat_item_press" />

    <Button
        android:id="@+id/music_shuffl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/playingtop"
        android:background="@drawable/shuffle_item_press" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/playingtop"
        android:layout_centerInParent="true" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/default_play_activity_bg1" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/seekbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/kongzhi"
        android:background="@android:color/white" >

        <SeekBar
            android:id="@+id/audioTrack"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dp"
            android:paddingTop="4dp"
            android:progressDrawable="@drawable/seekbar_drawable"
            android:thumb="@drawable/seekbar_thumb" />

        <TextView
            android:id="@+id/current_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/audioTrack"
            android:text="00:00"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/final_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/audioTrack"
            android:text="00:00"
            android:textColor="@android:color/black" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/kongzhi"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="64dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/music_last"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back_item_press" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="64dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/music_this"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/begin_item_press" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="64dp"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/music_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/go_item_press" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

对于UI的设计给大家参考一下,说实话,自己的美感太不行了,所以也是借鉴了好几个音乐播放器的界面。

言归正题:如何将内存卡中Mp3文件读取出来并显示在列表中?

先建立一个Mp3Info对象:

package com.ye_yun_lin.Activity;

public class Mp3Info {
	private long id; // 歌曲ID 
	private String title; // 歌曲名称 
	private String album; // 专辑 
	private long albumId;// 专辑ID
	private String displayName; // 显示名称 
	private String artist; // 歌手名称 
	private long duration; // 歌曲时长 
	private long size; // 歌曲大小 
	private String url; // 歌曲路径 
	private String lrcTitle; // 歌词名称
	private String lrcSize; // 歌词大小

	public Mp3Info() {
		super();
	}

	public Mp3Info(long id, String title, String album, long albumId,
			String displayName, String artist, long duration, long size,
			String url, String lrcTitle, String lrcSize) {
		super();
		this.id = id;
		this.title = title;
		this.album = album;
		this.albumId = albumId;
		this.displayName = displayName;
		this.artist = artist;
		this.duration = duration;
		this.size = size;
		this.url = url;
		this.lrcTitle = lrcTitle;
		this.lrcSize = lrcSize;
	}

	@Override
	public String toString() {
		return "Mp3Info [id=" + id + ", title=" + title + ", album=" + album
				+ ", albumId=" + albumId + ", displayName=" + displayName
				+ ", artist=" + artist + ", duration=" + duration + ", size="
				+ size + ", url=" + url + ", lrcTitle=" + lrcTitle
				+ ", lrcSize=" + lrcSize + "]";
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAlbum() {
		return album;
	}

	public void setAlbum(String album) {
		this.album = album;
	}

	public long getAlbumId() {
		return albumId;
	}

	public void setAlbumId(long albumId) {
		this.albumId = albumId;
	}

	public String getArtist() {
		return artist;
	}

	public void setArtist(String artist) {
		this.artist = artist;
	}

	public long getDuration() {
		return duration;
	}

	public void setDuration(long duration) {
		this.duration = duration;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getLrcTitle() {
		return lrcTitle;
	}

	public void setLrcTitle(String lrcTitle) {
		this.lrcTitle = lrcTitle;
	}

	public String getLrcSize() {
		return lrcSize;
	}

	public void setLrcSize(String lrcSize) {
		this.lrcSize = lrcSize;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
}

查询歌曲的信息,保存在list中:

public static List<Mp3Info> getMp3Infos(Context context) {
		Cursor cursor = context.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		for (int i = 0; i < cursor.getCount(); i++) {
			Mp3Info mp3Info = new Mp3Info();
			cursor.moveToNext();
			long id = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
			String title = cursor.getString((cursor
					.getColumnIndex(MediaStore.Audio.Media.TITLE)));// 音乐标题
			String artist = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.ARTIST));// 艺术家
			long duration = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media.DURATION));// 时长
			long size = cursor.getLong(cursor
					.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
			String url = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
			int isMusic = cursor.getInt(cursor
					.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));// 是否为音乐
			if (isMusic != 0) { // 只把音乐添加到集合当中
				mp3Info.setId(id);
				mp3Info.setTitle(title);
				mp3Info.setArtist(artist);
				mp3Info.setDuration(duration);
				mp3Info.setSize(size);
				mp3Info.setUrl(url);
				mp3Infos.add(mp3Info);
			}
		}
		return mp3Infos;
	}

填充ListView:

public void setListAdpter(List<Mp3Info> mp3Infos) {
		List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
		for (Iterator<Mp3Info> iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("title", mp3Info.getTitle());
			map.put("Artist", mp3Info.getArtist());
			map.put("duration", MediaUtil.Format(mp3Info.getDuration()));
			map.put("size", String.valueOf(mp3Info.getSize()));
			map.put("url", mp3Info.getUrl());
			mp3list.add(map);
		}
		mAdapter = new SimpleAdapter(this, mp3list,
				R.layout.music_list_item_layout, new String[] { "title",
						"Artist", "duration" }, new int[] { R.id.music_title,
						R.id.music_Artist, R.id.music_duration });
		setListAdapter(mAdapter);
	}

上面的MediaUtil.format(),是将时间换算为00:00的方法:

public static String Format(long time) {
		SimpleDateFormat format = new SimpleDateFormat("mm:ss");
		String hmsString = format.format(time);
		return hmsString;
	}

最后效果为下



Android音乐播放器(2)--从SD卡中读取音乐,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。