home.xml:
001.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
002.
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
003.
xmlns:tools=
"http://schemas.android.com/tools"
004.
android:layout_width=
"fill_parent"
005.
android:layout_height=
"fill_parent"
006.
android:orientation=
"horizontal"
007.
tools:context=
".MainActivity"
>
008.
009.
<LinearLayout
010.
android:id=
"@+id/menu"
011.
android:layout_width=
"fill_parent"
012.
android:layout_height=
"fill_parent"
013.
android:background=
"@drawable/repeat_bg"
014.
android:gravity=
"bottom|center_horizontal"
015.
android:orientation=
"vertical"
>
016.
017.
<LinearLayout
018.
android:layout_width=
"match_parent"
019.
android:layout_height=
"50dp"
020.
android:orientation=
"horizontal"
021.
android:paddingBottom=
"10dp"
>
022.
023.
<LinearLayout
024.
android:layout_width=
"match_parent"
025.
android:layout_height=
"40dp"
026.
android:layout_weight=
"1"
>
027.
028.
<TextView
029.
android:layout_width=
"wrap_content"
030.
android:layout_height=
"wrap_content"
031.
android:paddingLeft=
"25dp"
032.
android:text=
"My Music"
033.
android:textColor=
"@android:color/black"
034.
android:textSize=
"35dp"
/>
035.
</LinearLayout>
036.
037.
<LinearLayout
038.
android:layout_width=
"match_parent"
039.
android:layout_height=
"40dp"
040.
android:layout_weight=
"3"
041.
android:gravity=
"center_horizontal"
>
042.
043.
<ImageView
044.
android:id=
"@+id/button1"
045.
android:layout_width=
"40dp"
046.
android:layout_height=
"40dp"
047.
android:background=
"@drawable/home_chilun_item_press"
048.
android:clickable=
"true"
/>
049.
</LinearLayout>
050.
</LinearLayout>
051.
052.
<RelativeLayout
053.
android:layout_width=
"wrap_content"
054.
android:layout_height=
"wrap_content"
055.
android:paddingBottom=
"25dp"
>
056.
057.
<ImageView
058.
android:id=
"@+id/listbutton"
059.
android:layout_width=
"110dp"
060.
android:layout_height=
"110dp"
061.
android:layout_marginRight=
"5dp"
062.
android:background=
"@drawable/list_item_press"
/>
063.
064.
<ImageView
065.
android:id=
"@+id/cloudbutton"
066.
android:layout_width=
"110dp"
067.
android:layout_height=
"110dp"
068.
android:layout_marginLeft=
"5dp"
069.
android:layout_toRightOf=
"@id/listbutton"
070.
android:background=
"@drawable/cloud_item_press"
/>
071.
072.
<ImageView
073.
android:id=
"@+id/lovebutton"
074.
android:layout_width=
"110dp"
075.
android:layout_height=
"110dp"
076.
android:layout_below=
"@id/listbutton"
077.
android:layout_marginTop=
"10dp"
078.
android:background=
"@drawable/love_item_press"
/>
079.
080.
<ImageView
081.
android:id=
"@+id/localbutton"
082.
android:layout_width=
"110dp"
083.
android:layout_height=
"110dp"
084.
android:layout_alignLeft=
"@id/cloudbutton"
085.
android:layout_alignTop=
"@id/lovebutton"
086.
android:background=
"@drawable/local_item_press"
/>
087.
088.
<ImageView
089.
android:id=
"@+id/cechebutton"
090.
android:layout_width=
"110dp"
091.
android:layout_height=
"110dp"
092.
android:layout_below=
"@id/lovebutton"
093.
android:layout_marginTop=
"10dp"
094.
android:background=
"@drawable/cache_item_press"
/>
095.
096.
<ImageView
097.
android:id=
"@+id/downloadbutton"
098.
android:layout_width=
"110dp"
099.
android:layout_height=
"110dp"
100.
android:layout_alignLeft=
"@id/localbutton"
101.
android:layout_alignTop=
"@id/cechebutton"
102.
android:background=
"@drawable/dowmload_item_press"
/>
103.
</RelativeLayout>
104.
105.
<LinearLayout
106.
android:layout_width=
"match_parent"
107.
android:layout_height=
"64dp"
108.
android:background=
"@drawable/mini_background"
109.
android:orientation=
"horizontal"
>
110.
111.
<ImageView
112.
android:layout_width=
"match_parent"
113.
android:layout_height=
"match_parent"
114.
android:layout_weight=
"2"
115.
android:src=
"@drawable/pan"
/>
116.
117.
<LinearLayout
118.
android:id=
"@+id/clickToPlaying"
119.
android:layout_width=
"match_parent"
120.
android:layout_height=
"match_parent"
121.
android:layout_weight=
"1"
122.
android:clickable=
"true"
123.
android:orientation=
"vertical"
124.
android:paddingLeft=
"5dp"
125.
android:paddingTop=
"12dp"
>
126.
127.
<TextView
128.
android:id=
"@+id/music_name"
129.
android:layout_width=
"wrap_content"
130.
android:layout_height=
"wrap_content"
131.
android:text=
"Music Map"
132.
android:textColor=
"@android:color/white"
133.
android:textSize=
"18dp"
/>
134.
135.
<TextView
136.
android:id=
"@+id/singer_name"
137.
android:layout_width=
"wrap_content"
138.
android:layout_height=
"wrap_content"
139.
android:text=
"音乐与你同在"
140.
android:textColor=
"@android:color/white"
141.
android:textSize=
"12dp"
/>
142.
</LinearLayout>
143.
144.
<ImageView
145.
android:id=
"@+id/homepause"
146.
android:layout_width=
"match_parent"
147.
android:layout_height=
"match_parent"
148.
android:layout_weight=
"2"
149.
android:background=
"@drawable/home_button_item"
150.
android:clickable=
"true"
/>
151.
</LinearLayout>
152.
</LinearLayout>
153.
154.
<LinearLayout
155.
android:id=
"@+id/content"
156.
android:layout_width=
"180dp"
157.
android:layout_height=
"match_parent"
158.
android:background=
"@drawable/content_background"
159.
android:orientation=
"vertical"
>
160.
161.
<RelativeLayout
162.
android:id=
"@+id/scanningtbutton"
163.
style=
"@style/Relativelayoutstyle"
>
164.
165.
<ImageView
166.
android:id=
"@+id/imageview1"
167.
style=
"@style/imageviewstyle"
168.
android:src=
"@drawable/music"
/>
169.
170.
<TextView
171.
style=
"@style/textviewstyle"
172.
android:layout_toRightOf=
"@id/imageview1"
173.
android:text=
"扫描音乐"
/>
174.
</RelativeLayout>
175.
176.
<RelativeLayout
177.
android:id=
"@+id/playingbutton"
178.
style=
"@style/Relativelayoutstyle"
>
179.
180.
<ImageView
181.
android:id=
"@+id/imageview2"
182.
style=
"@style/imageviewstyle"
183.
android:src=
"@drawable/musicplaying"
/>
184.
185.
<TextView
186.
android:id=
"@+id/statustext"
187.
style=
"@style/textviewstyle"
188.
android:layout_toRightOf=
"@id/imageview2"
189.
android:text=
"顺序播放"
/>
190.
</RelativeLayout>
191.
192.
<RelativeLayout
193.
android:id=
"@+id/sleepbutton"
194.
style=
"@style/Relativelayoutstyle"
>
195.
196.
<ImageView
197.
android:id=
"@+id/imageview3"
198.
style=
"@style/imageviewstyle"
199.
android:src=
"@drawable/sleep"
/>
200.
201.
<TextView
202.
style=
"@style/textviewstyle"
203.
android:layout_toRightOf=
"@id/imageview3"
204.
android:text=
"睡眠模式"
/>
205.
</RelativeLayout>
206.
207.
<RelativeLayout
208.
android:id=
"@+id/downbutton"
209.
style=
"@style/Relativelayoutstyle"
>
210.
211.
<ImageView
212.
android:id=
"@+id/imageview4"
213.
style=
"@style/imageviewstyle"
214.
android:src=
"@drawable/down"
/>
215.
216.
<TextView
217.
style=
"@style/textviewstyle"
218.
android:layout_toRightOf=
"@id/imageview4"
219.
android:text=
"下载管理"
/>
220.
</RelativeLayout>
221.
222.
<RelativeLayout
223.
android:id=
"@+id/soundbutton"
224.
style=
"@style/Relativelayoutstyle"
>
225.
226.
<ImageView
227.
android:id=
"@+id/imageview5"
228.
style=
"@style/imageviewstyle"
229.
android:src=
"@drawable/vivo"
/>
230.
231.
<TextView
232.
style=
"@style/textviewstyle"
233.
android:layout_toRightOf=
"@id/imageview5"
234.
android:text=
"音效"
/>
235.
</RelativeLayout>
236.
237.
<RelativeLayout
238.
android:id=
"@+id/managerbutton"
239.
style=
"@style/Relativelayoutstyle"
>
240.
241.
<ImageView
242.
android:id=
"@+id/imageview6"
243.
style=
"@style/imageviewstyle"
244.
android:src=
"@drawable/maneger"
/>
245.
246.
<TextView
247.
style=
"@style/textviewstyle"
248.
android:layout_toRightOf=
"@id/imageview6"
249.
android:text=
"设置"
/>
250.
</RelativeLayout>
251.
252.
<RelativeLayout
253.
android:id=
"@+id/aboutbutton"
254.
style=
"@style/Relativelayoutstyle"
>
255.
256.
<ImageView
257.
android:id=
"@+id/imageview7"
258.
style=
"@style/imageviewstyle"
259.
android:src=
"@drawable/man"
/>
260.
261.
<TextView
262.
style=
"@style/textviewstyle"
263.
android:layout_width=
"wrap_content"
264.
android:layout_toRightOf=
"@id/imageview7"
265.
android:text=
"关于我们"
/>
266.
267.
</RelativeLayout>
268.
</LinearLayout>
269.
270.
</LinearLayout>
music_list.xml:
01.
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
02.
xmlns:tools=
"http://schemas.android.com/tools"
03.
android:id=
"@+id/RelativeLayout1"
04.
android:layout_width=
"match_parent"
05.
android:layout_height=
"match_parent"
06.
android:background=
"#ecebd7"
>
07.
<RelativeLayout
08.
android:id=
"@+id/top"
09.
android:layout_width=
"fill_parent"
10.
android:layout_height=
"50dp"
11.
android:background=
"@drawable/main_top_bar_bg"
>
12.
<ImageView
13.
android:id=
"@+id/backtohome"
14.
android:layout_width=
"60dp"
15.
android:layout_height=
"50dp"
16.
android:clickable=
"true"
17.
android:background=
"@drawable/activity_item_back"
/>
18.
19.
<TextView
20.
android:layout_width=
"wrap_content"
21.
android:layout_height=
"wrap_content"
22.
android:text=
"所有歌曲"
23.
android:textSize=
"25dp"
24.
android:layout_centerInParent=
"true"
25.
android:textColor=
"@android:color/black"
/>
26.
</RelativeLayout>
27.
<ListView
28.
android:layout_below=
"@id/top"
29.
android:padding=
"5dp"
30.
android:id=
"@android:id/list"
31.
android:layout_width=
"match_parent"
32.
android:layout_height=
"wrap_content"
>
33.
</ListView>
34.
</RelativeLayout>
music_start.xml:
001.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
002.
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
003.
android:layout_width=
"match_parent"
004.
android:layout_height=
"match_parent"
005.
android:background=
"#ecebd7"
>
006.
007.
<RelativeLayout
008.
android:id=
"@+id/playingtop"
009.
android:layout_width=
"fill_parent"
010.
android:layout_height=
"50dp"
011.
android:background=
"@drawable/main_top_bar_bg"
>
012.
013.
<ImageView
014.
android:id=
"@+id/back"
015.
android:layout_width=
"60dp"
016.
android:layout_height=
"50dp"
017.
android:background=
"@drawable/activity_item_back"
018.
android:clickable=
"true"
/>
019.
020.
<TextView
021.
android:id=
"@+id/music_start_name"
022.
android:layout_width=
"wrap_content"
023.
android:layout_height=
"wrap_content"
024.
android:layout_centerInParent=
"true"
025.
android:text=
"歌曲"
026.
android:textColor=
"@android:color/black"
027.
android:textSize=
"20dp"
/>
028.
</RelativeLayout>
029.
030.
<Button
031.
android:id=
"@+id/music_repeat"
032.
android:layout_width=
"wrap_content"
033.
android:layout_height=
"wrap_content"
034.
android:layout_below=
"@id/playingtop"
035.
android:background=
"@drawable/repeat_item_press"
/>
036.
037.
<Button
038.
android:id=
"@+id/music_shuffl"
039.
android:layout_width=
"wrap_content"
040.
android:layout_height=
"wrap_content"
041.
android:layout_alignParentRight=
"true"
042.
android:layout_below=
"@id/playingtop"
043.
android:background=
"@drawable/shuffle_item_press"
/>
044.
045.
<LinearLayout
046.
android:layout_width=
"match_parent"
047.
android:layout_height=
"wrap_content"
048.
android:layout_below=
"@id/playingtop"
049.
android:layout_centerInParent=
"true"
>
050.
051.
<ImageView
052.
android:layout_width=
"match_parent"
053.
android:layout_height=
"wrap_content"
054.
android:scaleType=
"fitCenter"
055.
android:src=
"@drawable/default_play_activity_bg1"
/>
056.
</LinearLayout>
057.
058.
<RelativeLayout
059.
android:id=
"@+id/seekbarLayout"
060.
android:layout_width=
"match_parent"
061.
android:layout_height=
"wrap_content"
062.
android:layout_above=
"@+id/kongzhi"
063.
android:background=
"@android:color/white"
>
064.
065.
<SeekBar
066.
android:id=
"@+id/audioTrack"
067.
android:layout_width=
"match_parent"
068.
android:layout_height=
"wrap_content"
069.
android:paddingBottom=
"2dp"
070.
android:paddingTop=
"4dp"
071.
android:progressDrawable=
"@drawable/seekbar_drawable"
072.
android:thumb=
"@drawable/seekbar_thumb"
/>
073.
074.
<TextView
075.
android:id=
"@+id/current_progress"
076.
android:layout_width=
"wrap_content"
077.
android:layout_height=
"wrap_content"
078.
android:layout_below=
"@id/audioTrack"
079.
android:text=
"00:00"
080.
android:textColor=
"@android:color/black"
/>
081.
082.
<TextView
083.
android:id=
"@+id/final_progress"
084.
android:layout_width=
"wrap_content"
085.
android:layout_height=
"wrap_content"
086.
android:layout_alignParentRight=
"true"
087.
android:layout_below=
"@id/audioTrack"
088.
android:text=
"00:00"
089.
android:textColor=
"@android:color/black"
/>
090.
</RelativeLayout>
091.
092.
<LinearLayout
093.
android:id=
"@+id/kongzhi"
094.
android:layout_width=
"match_parent"
095.
android:layout_height=
"64dp"
096.
android:layout_alignParentBottom=
"true"
097.
android:layout_centerHorizontal=
"true"
098.
android:background=
"@android:color/white"
099.
android:orientation=
"horizontal"
>
100.
101.
<LinearLayout
102.
android:layout_width=
"wrap_content"
103.
android:layout_height=
"64dp"
104.
android:layout_weight=
"1"
105.
android:gravity=
"center_horizontal"
>
106.
107.
<Button
108.
android:id=
"@+id/music_last"
109.
android:layout_width=
"wrap_content"
110.
android:layout_height=
"wrap_content"
111.
android:background=
"@drawable/back_item_press"
/>
112.
</LinearLayout>
113.
114.
<LinearLayout
115.
android:layout_width=
"wrap_content"
116.
android:layout_height=
"64dp"
117.
android:layout_weight=
"1"
118.
android:gravity=
"center_horizontal"
>
119.
120.
<Button
121.
android:id=
"@+id/music_this"
122.
android:layout_width=
"wrap_content"
123.
android:layout_height=
"wrap_content"
124.
android:background=
"@drawable/begin_item_press"
/>
125.
</LinearLayout>
126.
127.
<LinearLayout
128.
android:layout_width=
"wrap_content"
129.
android:layout_height=
"64dp"
130.
android:layout_weight=
"1"
131.
android:gravity=
"center_horizontal"
>
132.
133.
<Button
134.
android:id=
"@+id/music_next"
135.
android:layout_width=
"wrap_content"
136.
android:layout_height=
"wrap_content"
137.
android:background=
"@drawable/go_item_press"
/>
138.
</LinearLayout>
139.
</LinearLayout>
140.
141.
</RelativeLayout>
对于UI的设计给大家参考一下,说实话,自己的美感太不行了,所以也是借鉴了好几个音乐播放器的界面。
言归正题:如何将内存卡中Mp3文件读取出来并显示在列表中?
先建立一个Mp3Info对象:
001.
package
com.ye_yun_lin.Activity;
002.
003.
public
class
Mp3Info {
004.
private
long
id;
// 歌曲ID
005.
private
String title;
// 歌曲名称
006.
private
String album;
// 专辑
007.
private
long
albumId;
// 专辑ID
008.
private
String displayName;
// 显示名称
009.
private
String artist;
// 歌手名称
010.
private
long
duration;
// 歌曲时长
011.
private
long
size;
// 歌曲大小
012.
private
String url;
// 歌曲路径
013.
private
String lrcTitle;
// 歌词名称
014.
private
String lrcSize;
// 歌词大小
015.
016.
public
Mp3Info() {
017.
super
();
018.
}
019.
020.
public
Mp3Info(
long
id, String title, String album,
long
albumId,
021.
String displayName, String artist,
long
duration,
long
size,
022.
String url, String lrcTitle, String lrcSize) {
023.
super
();
024.
this
.id = id;
025.
this
.title = title;
026.
this
.album = album;
027.
this
.albumId = albumId;
028.
this
.displayName = displayName;
029.
this
.artist = artist;
030.
this
.duration = duration;
031.
this
.size = size;
032.
this
.url = url;
033.
this
.lrcTitle = lrcTitle;
034.
this
.lrcSize = lrcSize;
035.
}
036.
037.
@Override
038.
public
String toString() {
039.
return
"Mp3Info [id="
+ id +
", title="
+ title +
", album="
+ album
040.
+
", albumId="
+ albumId +
", displayName="
+ displayName
041.
+
", artist="
+ artist +
", duration="
+ duration +
", size="
042.
+ size +
", url="
+ url +
", lrcTitle="
+ lrcTitle
043.
+
", lrcSize="
+ lrcSize +
"]"
;
044.
}
045.
046.
public
long
getId() {
047.
return
id;
048.
}
049.
050.
public
void
setId(
long
id) {
051.
this
.id = id;
052.
}
053.
054.
public
String getTitle() {
055.
return
title;
056.
}
057.
058.
public
void
setTitle(String title) {
059.
this
.title = title;
060.
}
061.
062.
public
String getAlbum() {
063.
return
album;
064.
}
065.
066.
public
void
setAlbum(String album) {
067.
this
.album = album;
068.
}
069.
070.
public
long
getAlbumId() {
071.
return
albumId;
072.
}
073.
074.
public
void
setAlbumId(
long
albumId) {
075.
this
.albumId = albumId;
076.
}
077.
078.
public
String getArtist() {
079.
return
artist;
080.
}
081.
082.
public
void
setArtist(String artist) {
083.
this
.artist = artist;
084.
}
085.
086.
public
long
getDuration() {
087.
return
duration;
088.
}
089.
090.
public
void
setDuration(
long
duration) {
091.
this
.duration = duration;
092.
}
093.
094.
public
long
getSize() {
095.
return
size;
096.
}
097.
098.
public
void
setSize(
long
size) {
099.
this
.size = size;
100.
}
101.
102.
public
String getUrl() {
103.
return
url;
104.
}
105.
106.
public
void
setUrl(String url) {
107.
this
.url = url;
108.
}
109.
110.
public
String getLrcTitle() {
111.
return
lrcTitle;
112.
}
113.
114.
public
void
setLrcTitle(String lrcTitle) {
115.
this
.lrcTitle = lrcTitle;
116.
}
117.
118.
public
String getLrcSize() {
119.
return
lrcSize;
120.
}
121.
122.
public
void
setLrcSize(String lrcSize) {
123.
this
.lrcSize = lrcSize;
124.
}
125.
126.
public
String getDisplayName() {
127.
return
displayName;
128.
}
129.
130.
public
void
setDisplayName(String displayName) {
131.
this
.displayName = displayName;
132.
}
133.
}
查询歌曲的信息,保存在list中:
01.
public
static
List<Mp3Info> getMp3Infos(Context context) {
02.
Cursor cursor = context.getContentResolver().query(
03.
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null
,
null
,
null
,
04.
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
05.
List<Mp3Info> mp3Infos =
new
ArrayList<Mp3Info>();
06.
for
(
int
i =
0
; i < cursor.getCount(); i++) {
07.
Mp3Info mp3Info =
new
Mp3Info();
08.
cursor.moveToNext();
09.
long
id = cursor.getLong(cursor
10.
.getColumnIndex(MediaStore.Audio.Media._ID));
// 音乐id
11.
String title = cursor.getString((cursor
12.
.getColumnIndex(MediaStore.Audio.Media.TITLE)));
// 音乐标题
13.
String artist = cursor.getString(cursor
14.
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
// 艺术家
15.
long
duration = cursor.getLong(cursor
16.
.getColumnIndex(MediaStore.Audio.Media.DURATION));
// 时长
17.
long
size = cursor.getLong(cursor
18.
.getColumnIndex(MediaStore.Audio.Media.SIZE));
// 文件大小
19.
String url = cursor.getString(cursor
20.
.getColumnIndex(MediaStore.Audio.Media.DATA));
// 文件路径
21.
int
isMusic = cursor.getInt(cursor
22.
.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
// 是否为音乐
23.
if
(isMusic !=
0
) {
// 只把音乐添加到集合当中
24.
mp3Info.setId(id);
25.
mp3Info.setTitle(title);
26.
mp3Info.setArtist(artist);
27.
mp3Info.setDuration(duration);
28.
mp3Info.setSize(size);
29.
mp3Info.setUrl(url);
30.
mp3Infos.add(mp3Info);
31.
}
32.
}
33.
return
mp3Infos;
34.
}
填充ListView:
01.
public
void
setListAdpter(List<Mp3Info> mp3Infos) {
02.
List<HashMap<String, String>> mp3list =
new
ArrayList<HashMap<String, String>>();
03.
for
(Iterator<Mp3Info> iterator = mp3Infos.iterator(); iterator.hasNext();) {
04.
Mp3Info mp3Info = (Mp3Info) iterator.next();
05.
HashMap<String, String> map =
new
HashMap<String, String>();
06.
map.put(
"title"
, mp3Info.getTitle());
07.
map.put(
"Artist"
, mp3Info.getArtist());
08.
map.put(
"duration"
, MediaUtil.Format(mp3Info.getDuration()));
09.
map.put(
"size"
, String.valueOf(mp3Info.getSize()));
10.
map.put(
"url"
, mp3Info.getUrl());
11.
mp3list.add(map);
12.
}
13.
mAdapter =
new
SimpleAdapter(
this
, mp3list,
14.
R.layout.music_list_item_layout,
new
String[] {
"title"
,
15.
"Artist"
,
"duration"
},
new
int
[] { R.id.music_title,
16.
R.id.music_Artist, R.id.music_duration });
17.
setListAdapter(mAdapter);
18.
}
上面的MediaUtil.format(),是将时间换算为00:00的方法:
1.
public
static
String Format(
long
time) {
2.
SimpleDateFormat format =
new
SimpleDateFormat(
"mm:ss"
);
3.
String hmsString = format.format(time);
4.
return
hmsString;
5.
}
最后效果为下: