思路


首先是扫描本地所有的音频文件,然后全部装进集合当中,接下来就是用ListView展示在屏幕上,大概就是这几个步骤了,接下来细讲



创建一个容器


进行过数据解析的朋友都应该知道JavaBean吧,用来装载解析出来的数据,我们这里同样也要创建一个JavaBean,用来装载扫描到的音频文件,具体的代码是:


1. <pre name="code" class="html">package com.duanlian.mymusicplayerdemo.bean;  
2.   
3. /**  
4.  * Created by user on 2016/6/24.  
5.  * 放置音乐  
6.  */  
7. public class Song {  
8.     /**  
9.      * 歌手  
10.      */  
11.     public String singer;  
12.     /**  
13.      * 歌曲名  
14.      */  
15.     public String song;  
16.     /**  
17.      * 歌曲的地址  
18.      */  
19.     public String path;  
20.     /**  
21.      * 歌曲长度  
22.      */  
23.     public int duration;  
24.     /**  
25.      * 歌曲的大小  
26.      */  
27.     public long size;  
28. }


创建一个用来扫描本地音频的工具类


类里面定义一个扫描本地音频的方法,其返回值是一个list集合,集合里面装的泛型就是上一步创建的JavaBean,具体代码如下:



1. package com.duanlian.mymusicplayerdemo.utils;  
2.   
3. import android.content.Context;  
4. import android.database.Cursor;  
5. import android.provider.MediaStore;  
6.   
7. import com.duanlian.mymusicplayerdemo.bean.Song;  
8.   
9. import java.util.ArrayList;  
10. import java.util.List;  
11.   
12.   
13. /**  
14.  * 音乐工具类,  
15.  */  
16. public class MusicUtils {  
17.     /**  
18.      * 扫描系统里面的音频文件,返回一个list集合  
19.      */  
20. <Song> getMusicData(Context context) {  
21. <Song> list = new ArrayList<Song>();  
22.         // 媒体库查询语句(写一个工具类MusicUtils)  
23. cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,  
24.                 null, MediaStore.Audio.AudioColumns.IS_MUSIC);  
25.         if (cursor != null) {  
26.             while (cursor.moveToNext()) {  
27. song = new Song();  
28. song.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));  
29. song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));  
30. song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));  
31. song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));  
32. song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));  
33. > 1000 * 800) {  
34.                     // 注释部分是切割标题,分离出歌曲名和歌手 (本地媒体库读取的歌曲信息不规范)  
35.                     if (song.song.contains("-")) {  
36. str = song.song.split("-");  
37. song.singer = str[0];  
38. song.song = str[1];  
39.                     }  
40.                     list.add(song);  
41.                 }  
42.             }  
43.             // 释放资源  
44.             cursor.close();  
45.         }  
46.   
47.         return list;  
48.     }  
49.   
50.     /**  
51.      * 定义一个方法用来格式化获取到的时间  
52.      */  
53.     public static String formatTime(int time) {  
54. < 10) {  
55.             return time / 1000 / 60 + ":0" + time / 1000 % 60;  
56.   
57.         } else {  
58.             return time / 1000 / 60 + ":" + time / 1000 % 60;  
59.         }  
60.   
61.     }  
62. }


1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


数据展示


ListView展示数据需要配合Adapter一起使用,而Adapter里面需要传一个上下文,一个数据源,上下文直接能拿到,数据源上面我们已经拿到了,接下来是写ListView和Adapter的编写,先看看MainActivity的布局文件,非常简单:


1. <?xml version="1.0" encoding="utf-8"?>  
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3. xmlns:tools="http://schemas.android.com/tools"  
4. android:layout_width="match_parent"  
5. android:layout_height="match_parent"  
6. tools:context="com.duanlian.mymusicplayerdemo.MainActivity">  
7.   
8. <TextView  
9. android:id="@+id/main_textview"  
10. android:layout_width="match_parent"  
11. android:layout_height="40dp"  
12. android:gravity="center"  
13. android:text="我的音乐" />  
14. <ListView  
15. android:id="@+id/main_listview"  
16. android:layout_below="@+id/main_textview"  
17. android:layout_width="match_parent"  
18. android:layout_height="match_parent"/>  
19. </RelativeLayout>


接下来就是MainActivity里面的编写,具体代码:




1. package com.duanlian.mymusicplayerdemo;  
2.   
3. import android.support.v7.app.AppCompatActivity;  
4. import android.os.Bundle;  
5. import android.widget.ListView;  
6.   
7. import com.duanlian.mymusicplayerdemo.adapter.MyAdapter;  
8. import com.duanlian.mymusicplayerdemo.bean.Song;  
9. import com.duanlian.mymusicplayerdemo.utils.MusicUtils;  
10.   
11. import java.util.ArrayList;  
12. import java.util.List;  
13.   
14. public class MainActivity extends AppCompatActivity {  
15.   
16.     private ListView mListView;  
17. <Song> list;  
18.     private MyAdapter adapter;  
19.   
20.     @Override  
21.     protected void onCreate(Bundle savedInstanceState) {  
22.         super.onCreate(savedInstanceState);  
23.         setContentView(R.layout.activity_main);  
24.         initView();  
25.     }  
26.   
27.     /**  
28.      * 初始化view  
29.      */  
30.     private void initView() {  
31. mListView = (ListView) findViewById(R.id.main_listview);  
32. list = new ArrayList<>();  
33.         //把扫描到的音乐赋值给list  
34. list = MusicUtils.getMusicData(this);  
35. adapter = new MyAdapter(this,list);  
36.         mListView.setAdapter(adapter);  
37.     }  
38. }

是不是很简单,就定义一个ListView并且绑定Adapter,接下来就Adapter得编写:


1. package com.duanlian.mymusicplayerdemo.adapter;  
2.   
3. import android.content.Context;  
4. import android.view.View;  
5. import android.view.ViewGroup;  
6. import android.widget.BaseAdapter;  
7. import android.widget.TextView;  
8.   
9. import com.duanlian.mymusicplayerdemo.MainActivity;  
10. import com.duanlian.mymusicplayerdemo.R;  
11. import com.duanlian.mymusicplayerdemo.bean.Song;  
12. import com.duanlian.mymusicplayerdemo.utils.MusicUtils;  
13.   
14. import java.util.List;  
15.   
16. public class MyAdapter extends BaseAdapter {  
17.     private Context context;  
18. <Song> list;  
19. <Song> list) {  
20. this.context = mainActivity;  
21. this.list = list;  
22.   
23.     }  
24.   
25.     @Override  
26.     public int getCount() {  
27.         return list.size();  
28.     }  
29.   
30.     @Override  
31.     public Object getItem(int i) {  
32.         return list.get(i);  
33.     }  
34.   
35.     @Override  
36.     public long getItemId(int i) {  
37.         return i;  
38.     }  
39.   
40.     @Override  
41.     public View getView(int i, View view, ViewGroup viewGroup) {  
42. holder = null;  
43. view == null) {  
44. holder = new ViewHolder();  
45.             //引入布局  
46. view = View.inflate(context, R.layout.item_music_listview, null);  
47.             //实例化对象  
48. holder.song = (TextView) view.findViewById(R.id.item_mymusic_song);  
49. holder.singer = (TextView) view.findViewById(R.id.item_mymusic_singer);  
50. holder.duration = (TextView) view.findViewById(R.id.item_mymusic_duration);  
51. holder.position = (TextView) view.findViewById(R.id.item_mymusic_postion);  
52.             view.setTag(holder);  
53.         } else {  
54. holder = (ViewHolder) view.getTag();  
55.         }  
56.         //给控件赋值  
57.         holder.song.setText(list.get(i).song.toString());  
58.         holder.singer.setText(list.get(i).singer.toString());  
59.         //时间需要转换一下  
60. duration = list.get(i).duration;  
61. time = MusicUtils.formatTime(duration);  
62.         holder.duration.setText(time);  
63.         holder.position.setText(i+1+"");  
64.   
65.         return view;  
66.     }  
67.     class ViewHolder{  
68.         TextView song;//歌曲名  
69.         TextView singer;//歌手  
70.         TextView duration;//时长  
71.         TextView position;//序号  
72.   
73.     }  
74.   
75. }



ListView都需要一个item,具体代码:


1. <?xml version="1.0" encoding="utf-8"?>  
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3. android:layout_width="match_parent"  
4. android:layout_height="110dp"  
5. android:orientation="horizontal"  
6. android:gravity="center_vertical"  
7. android:padding="5dp">  
8. <TextView  
9. android:id="@+id/item_mymusic_postion"  
10. android:layout_width="wrap_content"  
11. android:layout_height="wrap_content"  
12. android:layout_margin="10dp"  
13. android:text="1"  
14. android:gravity="center"  
15. android:textColor="#0d0c0c"  
16. android:textSize="18sp" />  
17.   
18. <RelativeLayout  
19. android:layout_width="wrap_content"  
20. android:layout_height="wrap_content"  
21. android:layout_marginLeft="20dp"  
22. >  
23.   
24. <TextView  
25. android:id="@+id/item_mymusic_song"  
26. android:layout_width="wrap_content"  
27. android:layout_height="wrap_content"  
28. android:text="歌曲名"  
29. android:textColor="#0d0c0c"  
30. android:textSize="20dp" />  
31.   
32.   
33. <TextView  
34. android:id="@+id/item_mymusic_singer"  
35. android:layout_width="wrap_content"  
36. android:layout_height="wrap_content"  
37. android:layout_below="@+id/item_mymusic_song"  
38. android:text="歌手"  
39. android:textColor="#0d0c0c"  
40. android:textSize="15sp" />  
41.   
42. <TextView  
43. android:id="@+id/item_mymusic_duration"  
44. android:layout_width="wrap_content"  
45. android:layout_height="wrap_content"  
46. android:layout_below="@+id/item_mymusic_song"  
47. android:layout_marginLeft="15dp"  
48. android:textColor="#0d0c0c"  
49. android:layout_toRightOf="@+id/item_mymusic_singer"  
50. android:text="时间"  
51. android:textSize="15sp" />  
52. </RelativeLayout>  
53.   
54. </LinearLayout>