Mp3Info.java 如下:
package com.iwanghang.drmplayer.vo; /** * Created by iwanghang on 16/4/16. */ public class Mp3Info { private long id; private String title;//歌名 private String artist;//艺术家 private String album;//专辑 private long albumId;//专辑id private long duration;//时长 private long size;//大小 private String url;//路径 private int isMusic;//是否为音乐 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 getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } 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 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 int getIsMusic() { return isMusic; } public void setIsMusic(int isMusic) { this.isMusic = isMusic; } @Override public String toString() { return "Mp3Info{" + "id=" + id + ", title='" + title + '\'' + ", artist='" + artist + '\'' + ", album='" + album + '\'' + ", albumId=" + albumId + ", duration=" + duration + ", size=" + size + ", url='" + url + '\'' + ", isMusic=" + isMusic + '}'; } }<span id="transmark"></span>
MediaUtils.java 如下:
package com.iwanghang.drmplayer.utils; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import com.iwanghang.drmplayer.vo.Mp3Info; import java.util.ArrayList; /** * Created by iwanghang on 16/4/16. */ public class MediaUtils { // 获取专辑封面的Uri private static final Uri albumArtUri = Uri .parse("content://media/external/audio/albumart"); /** * 根据歌曲id查询歌曲信息 */ public static Mp3Info getMp3Info(Context context, long _id){ System.out.println("MediaUtils.java #0 : " + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); Cursor cursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media._ID + "=" + _id, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); Mp3Info mp3Info = null; if(cursor.moveToNext()){ mp3Info = new Mp3Info(); 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));//艺术家 String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑 long albumid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id 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.setAlbum(album); mp3Info.setAlbumId(albumid); mp3Info.setDuration(duration); mp3Info.setSize(size); mp3Info.setUrl(url); System.out.println("MediaUtils.java #101 : title = " + title + " | artist = " + artist + " | duration = " + duration); System.out.println("MediaUtils.java #102 : id = " + id + " | album = " + album + " | size = " + size); System.out.println("MediaUtils.java #103 : url = " + url); } } cursor.close(); return mp3Info; } /** * 用于从数据库中查询歌曲的信息,保存在List当中 */ public static long[] getMp3InfoIds(Context context){ Cursor cursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Audio.Media._ID}, MediaStore.Audio.Media.DURATION + ">=10000", null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); long[] ids = null; if(cursor != null){ ids = new long[cursor.getCount()]; for (int i = 0;i<cursor.getCount();i++){ cursor.moveToNext(); ids[i] = cursor.getLong(0); } } cursor.close(); return ids; } /** * 用于从数据库中查询歌曲的信息,保存在List当中 */ public static ArrayList<Mp3Info> getMp3Infos(Context context) { System.out.println("MediaUtils.java #2 : " + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); Cursor cursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media.DURATION + ">=10000", null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>(); System.out.println("MediaUtils.java #3 :cursor.getCount() : " + cursor.getCount()); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToNext(); Mp3Info mp3Info = new Mp3Info(); 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));//艺术家 String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑 long albumid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id 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.setAlbum(album); mp3Info.setAlbumId(albumid); mp3Info.setDuration(duration); mp3Info.setSize(size); mp3Info.setUrl(url); mp3Infos.add(mp3Info); System.out.println("MediaUtils.java #401 : title = " + title + " | artist = " + artist + " | duration = " + duration); System.out.println("MediaUtils.java #402 : id = " + id + " | album = " + album + " | size = " + size); System.out.println("MediaUtils.java #403 : url = " + url); System.out.println("MediaUtils.java #404 : mp3Infos = " + mp3Infos.size()); } } cursor.close(); System.out.println("MediaUtils.java #405 : mp3Infos = " + mp3Infos.size()); return mp3Infos; } /** * 格式化时间,将毫秒转换为分:秒格式 */ public static String formatTime(long time){ String min = time / (1000*60) + ""; String sec = time % (1000*60) + ""; if (min.length()<2){ min = "0" + time / (1000*60) + ""; }else { min = time / (1000*60) + ""; } if (sec.length() == 4){ sec = "0" + time % (1000*60) + ""; }else if (sec.length() == 3){ sec = "00" + time % (1000*60) + ""; }else if (sec.length() == 2){ sec = "000" + time % (1000*60) + ""; }else if (sec.length() == 1) { sec = "0000" + time % (1000 * 60) + ""; } return min + ":" + sec.trim().substring(0,2); } }
MyMusicListAdapter.java 如下:
package com.iwanghang.drmplayer.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.iwanghang.drmplayer.R; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import java.util.ArrayList; /** * Created by iwanghang on 16/4/16. */ public class MyMusicListAdapter extends BaseAdapter{ private Context ctx; private ArrayList<Mp3Info> mp3Infos; public MyMusicListAdapter(Context ctx, ArrayList<Mp3Info> mp3Infos){ this.ctx = ctx; this.mp3Infos = mp3Infos; System.out.println("MyMusicListAdapter.java #0 : ctx = " + ctx + ",mp3Infos = " + mp3Infos.size()); } public ArrayList<Mp3Info> getMp3Infos() { System.out.println("MyMusicListAdapter.java #1 : public ArrayList<Mp3Info> getMp3Infos() {"); return mp3Infos; } public void setMp3Infos(ArrayList<Mp3Info> mp3Infos) { System.out.println("MyMusicListAdapter.java #2 : public void setMp3Infos(ArrayList<Mp3Info> mp3Infos) {"); this.mp3Infos = mp3Infos; } @Override public int getCount() { System.out.println("MyMusicListAdapter.java #3 : public int getCount() {" + mp3Infos.size()); //return mp3Infos.size(); return mp3Infos.size(); } @Override public Object getItem(int position) { System.out.println("MyMusicListAdapter.java #4 : public Object getItem(int position) {"); return mp3Infos.get(position); } @Override public long getItemId(int position) { System.out.println("MyMusicListAdapter.java #5 : public long getItemId(int position) {"); return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { System.out.println("MyMusicListAdapter.java #6 : public View getView "); ViewHolder vh; if(convertView==null){ convertView = LayoutInflater.from(ctx).inflate(R.layout.item_music_list,null); vh = new ViewHolder(); vh.textView1_title = (TextView) convertView.findViewById(R.id.textView1_title); vh.textView2_singer = (TextView) convertView.findViewById(R.id.textView2_singer); vh.textView3_time = (TextView) convertView.findViewById(R.id.textView3_time); System.out.println("MyMusicListAdapter.java #7 : textView1_title = " + vh.textView1_title); convertView.setTag(vh); } vh = (ViewHolder) convertView.getTag(); Mp3Info mp3Info = mp3Infos.get(position); vh.textView1_title.setText(mp3Info.getTitle()); vh.textView2_singer.setText(mp3Info.getArtist()); vh.textView3_time.setText(MediaUtils.formatTime(mp3Info.getDuration())); return convertView; } static class ViewHolder{ TextView textView1_title; TextView textView2_singer; TextView textView3_time; } } //inflate(R.layout.item_music_list,null);
MyMusicListFragment 如下:
package com.iwanghang.drmplayer; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.iwanghang.drmplayer.adapter.MyMusicListAdapter; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import java.util.ArrayList; /** * Created by iwanghang on 16/4/16. */ public class MyMusicListFragment extends Fragment { private ListView listView_my_music; private ArrayList<Mp3Info> mp3Infos; private MyMusicListAdapter myMusicListAdapter; private MainActivity mainActivity; @Override public void onAttach(Context context) { super.onAttach(context); mainActivity = (MainActivity) context; } public static MyMusicListFragment newInstance() { MyMusicListFragment my = new MyMusicListFragment(); return my; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_music_list_layout,null); listView_my_music = (ListView) view.findViewById(R.id.listView_my_music); loadData(); return view; // return listView_my_music; } /** * 加载本地音乐列表 */ private void loadData(){ mp3Infos = MediaUtils.getMp3Infos(mainActivity); myMusicListAdapter = new MyMusicListAdapter(mainActivity,mp3Infos); listView_my_music.setAdapter(myMusicListAdapter); } public ListView getListView_my_music() { return listView_my_music; } public ArrayList<Mp3Info> getMp3Infos() { return mp3Infos; } public MyMusicListAdapter getMyMusicListAdapter() { return myMusicListAdapter; } public MainActivity getMainActivity() { return mainActivity; } }
activity_main.xml 如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.astuetz.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="48dip" android:background="@drawable/background_tabs" /> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tabs" tools:context=".MainActivity" /> <!--android:layout_above="@+id/colors"--> <!--<LinearLayout--> <!--android:id="@+id/colors"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="48dip"--> <!--android:layout_alignParentBottom="true"--> <!--android:layout_marginBottom="8dip"--> <!--android:layout_marginLeft="4dip"--> <!--android:layout_marginRight="4dip"--> <!--android:orientation="horizontal" >--> <!--<ImageView--> <!--android:layout_width="0dip"--> <!--android:layout_height="match_parent"--> <!--android:layout_margin="4dip"--> <!--android:layout_weight="1"--> <!--android:background="#FF666666"--> <!--android:onClick="onColorClicked"--> <!--android:tag="#FF666666" />--> <!--<ImageView--> <!--android:layout_width="0dip"--> <!--android:layout_height="match_parent"--> <!--android:layout_margin="4dip"--> <!--android:layout_weight="1"--> <!--android:background="#FF96AA39"--> <!--android:onClick="onColorClicked"--> <!--android:tag="#FF96AA39" />--> <!--<ImageView--> <!--android:layout_width="0dip"--> <!--android:layout_height="match_parent"--> <!--android:layout_margin="4dip"--> <!--android:layout_weight="1"--> <!--android:background="#FFC74B46"--> <!--android:onClick="onColorClicked"--> <!--android:tag="#FFC74B46" />--> <!--<ImageView--> <!--android:layout_width="0dip"--> <!--android:layout_height="match_parent"--> <!--android:layout_margin="4dip"--> <!--android:layout_weight="1"--> <!--android:background="#FFF4842D"--> <!--android:onClick="onColorClicked"--> <!--android:tag="#FFF4842D" />--> <!--<ImageView--> <!--android:layout_width="0dip"--> <!--android:layout_height="match_parent"--> <!--android:layout_margin="4dip"--> <!--android:layout_weight="1"--> <!--android:background="#FF3F9FE0"--> <!--android:onClick="onColorClicked"--> <!--android:tag="#FF3F9FE0" />--> <!--<ImageView--> <!--android:layout_width="0dip"--> <!--android:layout_height="match_parent"--> <!--android:layout_margin="4dip"--> <!--android:layout_weight="1"--> <!--android:background="#FF5161BC"--> <!--android:onClick="onColorClicked"--> <!--android:tag="#FF5161BC" />--> <!--</LinearLayout>--> </RelativeLayout>
my_music_list_layout.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="wrap_content" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView_my_music" android:layout_alignParentStart="true" android:layout_centerHorizontal="true" android:layout_above="@+id/linearLayout2" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:background="#2d2c2c" android:weightSum="1" android:layout_alignParentBottom="true" android:layout_alignParentStart="true"> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:id="@+id/imageView" android:src="@mipmap/app_icon" android:layout_alignParentTop = "true" android:layout_centerHorizontal = "true" android:layout_gravity="bottom" /> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:layout_margin="10dp" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="歌名" android:textColor="@android:color/white" android:id="@+id/textView_songName" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="歌手" android:textColor="@android:color/white" android:id="@+id/textView2_singer" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp"> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:id="@+id/imageView2_play_pause" android:layout_marginRight="10dp" android:src="@mipmap/app_play" /> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:id="@+id/imageView3_next" android:src="@mipmap/app_next" /> </LinearLayout> </LinearLayout> </RelativeLayout>
item_misic_layout.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" android:gravity="center" android:padding="10dp" > <ImageView android:id="@+id/imageView1_icon" android:layout_width="48dp" android:layout_height="48dp" android:src="@mipmap/app_icon" android:layout_marginRight="10dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/textView1_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textView1_title" /> <TextView android:id="@+id/textView2_singer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#a6a8a8" android:text="textView2_singer"/> </LinearLayout> <TextView android:id="@+id/textView3_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="textView3_time"/> </LinearLayout>