音乐播放器app(二)—主页面的分析与制作
主页面
使用PagerSlidingTabStrip开源框架作为主框架
PagerSlidingTabStrip下载
main_activity.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="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/id_main_activity_pagersliding"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/main_title_color"
/>
<android.support.v4.view.ViewPager
android:id="@+id/id_main_activity_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
</android.support.v4.view.ViewPager>
<!--一根灰色的线条-->
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#727272" />
<!--底部播放器的布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/my_music_buttom_color"
android:orientation="horizontal"
android:padding="5dp">
<!--专辑缩略图-->
<ImageButton
android:id="@+id/id_player_ib_musicpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@mipmap/music_pic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp">
<!--歌曲名-->
<TextView
android:id="@+id/id_player_tv_songs"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/songs"
android:textColor="#000000" />
<!--歌手名-->
<TextView
android:id="@+id/id_player_tv_artist"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/artist" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<!--播放-->
<ImageButton
android:id="@+id/id_player_ib_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@mipmap/play" />
<!--下一首-->
<ImageButton
android:id="@+id/id_player_ib_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingLeft="10dp"
android:src="@mipmap/next" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.astuetz.PagerSlidingTabStrip;
import com.musicplayer.adapter.MyPagerAdapter;
public class MainActivity extends FragmentActivity {
private PagerSlidingTabStrip tabs; //PagerSlidingTabStrip的tab
private ViewPager pager;
private MyPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
tabs = (PagerSlidingTabStrip) findViewById(R.id.id_main_activity_pagersliding);
pager = (ViewPager) findViewById(R.id.id_main_activity_viewpager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setViewPager(pager);
}
}
MyPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.musicplayer.fragment.MyMusicListFragment;
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"我的音乐","网络音乐"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public Fragment getItem(int position) {
return MyMusicListFragment.newInstance();
}
@Override
public int getCount() {
return TITLES.length;
}
}
MyMusicListFragment.java
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.musicplayer.activity.MainActivity;
import com.musicplayer.activity.R;
import com.musicplayer.adapter.MyMusicListAdapter;
import com.musicplayer.entity.Mp3Info;
import com.musicplayer.utils.MediaUtils;
import java.util.ArrayList;
public class MyMusicListFragment extends Fragment implements AdapterView.OnItemClickListener {
private ListView player_lv;
private ArrayList<Mp3Info> mp3Infos; //定义一个arraylist数组,存放MP3音乐
private MyMusicListAdapter myMusicListAdapter;
private MainActivity mainActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mainActivity = (MainActivity) context;
}
public static MyMusicListFragment newInstance(){
MyMusicListFragment mf = new MyMusicListFragment();
return mf;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_music_list,null);
player_lv = (ListView) view.findViewById(R.id.id_my_music_listview);
player_lv.setOnItemClickListener(this);
loadData();
return view;
}
/**
* 加载本地音乐数据
*/
private void loadData() {
mp3Infos = MediaUtils.getMp3Infos(mainActivity);
myMusicListAdapter = new MyMusicListAdapter(mainActivity,mp3Infos);
player_lv.setAdapter(myMusicListAdapter);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
MyMusicListAdapter.java
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.musicplayer.activity.R;
import com.musicplayer.entity.Mp3Info;
import com.musicplayer.utils.MediaUtils;
import java.util.ArrayList;
public class MyMusicListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Mp3Info> mMp3Info;
public MyMusicListAdapter(Context context,ArrayList<Mp3Info> mp3Infos){
this.mContext = context;
this.mMp3Info = mp3Infos;
}
public void setmMp3Info(ArrayList<Mp3Info> mMp3Info) {
this.mMp3Info = mMp3Info;
}
@Override
public int getCount() {
return mMp3Info.size();
}
@Override
public Object getItem(int i) {
return mMp3Info.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh;
if(view == null){
view = LayoutInflater.from(mContext).inflate(R.layout.music_list_item,null);
vh = new ViewHolder();
vh.songs = (TextView) view.findViewById(R.id.id_music_list_tv_song);
vh.singer = (TextView) view.findViewById(R.id.id_music_list_tv_singer);
vh.time = (TextView) view.findViewById(R.id.id_music_list_tv_time);
view.setTag(vh);
}
vh = (ViewHolder) view.getTag();
Mp3Info mp3Info = mMp3Info.get(i);
vh.songs.setText(mp3Info.getTitle());
vh.singer.setText(mp3Info.getArtist());
vh.time.setText(MediaUtils.formatTime(mp3Info.getDuration()));
return view;
}
static class ViewHolder{
TextView songs;
TextView singer;
TextView time;
}
}
Mp3Info.java
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;
}
}
MediaUtil.java
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import com.musicplayer.entity.Mp3Info;
import java.util.ArrayList;
/**
* 从本机查询音乐
*
* Created by Ryan on 2016/8/27.
*/
public class MediaUtils {
/**
* 根据歌曲id查询歌曲信息(单曲查询)
*
* @param context
* @param _id
* @return
*/
public static Mp3Info getMp3Info(Context context, long _id) {
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);
}
}
cursor.close();
return mp3Info;
}
/**
* 从数据库中查询歌曲的id,保存在List中
* 代码解析 MediaStore.Audio.Media.DURATION + ">=180000" 查询歌曲时间大于3分钟的
* @param context
* @return
*/
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 + ">=180000",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中
* @param context
* @return
*/
public static ArrayList<Mp3Info> getMp3Infos(Context context){
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,
MediaStore.Audio.Media.DURATION + ">=180000",null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
for(int i = 0;i<cursor.getCount();i++){
cursor.moveToNext();
Mp3Info mp3Info = new Mp3Info();
Long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._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);
}
}
cursor.close();
return mp3Infos;
}
/**
* 格式化时间,把毫秒转化为分:秒格式
* @param time
* @return
*/
public static String formatTime(long time){
String min = time / (1000 * 60) + ""; //计算分
String sec = time % (1000 * 60) + ""; //计算秒
//如果分钟少于两位数即(2:00),则变为(02:00)
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);
}
}
主界面的大体代码如上
MediaUtil.java文件是从手机上获取本地的音乐文件,查找出来,然后显示到MyMusicListFragment.java的ListView中
上面的代码比较简单,就不详细解说了,跟着我上面的代码顺序敲,我是一步一步发出来的代码。
另外,PagerSlidingTabStrip可以稍微修改,个人觉得原样式太丑,嘿嘿。
下一篇,音乐播放器app(三)—使用Service绑定Activity,让音乐播放