上期博客链接:https://blog.csdn.net/cungudafa/article/details/85318725
本次项目是在上次项目上的一个扩展,上次简单实现了音乐播放
与动画旋转
;
这次我们要实现读取本地mp3格式所以音乐
,以列表形式存放
,点击跳转&播放
。
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
分析:
1、封装类: 为了更好的管理我们的文件,我们需要单独建立一个类来管理MP3文件的属性值,新建类命名为MusicInfo.java
2、读取: 再新建一个类查询数据库取得MP3文件,并分离取得各属性值,MusicUtil.java
3、美化: 新建list.xml
用适配器MusicListAdapter.java
将读取内容存放于列表形式显示
4、显示:
package com.example.cungu.musicdemo;
//为了更好的管理我们的文件,我们需要单独建立一个类来管理MP3文件的属性值,新建类命名为MusicInfo.java:
public class MusicInfo {
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 MusicInfo() {
super();
}
public MusicInfo(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;
}
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;
}
}
package com.example.cungu.musicdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
//再新建一个类查询数据库取得MP3文件,并分离取得各属性值,MusicUtil.java:
public class MusicUtil {
public static List<MusicInfo> getMp3Infos(Context context) {
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
List<MusicInfo> musicInfos = new ArrayList<MusicInfo>();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToNext();
MusicInfo musicInfo = new MusicInfo();
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)); //专辑
String displayName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
long albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_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) { // 只把音乐添加到集合当中
musicInfo.setId(id);
musicInfo.setTitle(title);
musicInfo.setArtist(artist);
musicInfo.setAlbum(album);
musicInfo.setDisplayName(displayName);
musicInfo.setAlbumId(albumId);
musicInfo.setDuration(duration);
musicInfo.setSize(size);
musicInfo.setUrl(url);
musicInfos.add(musicInfo);
}
}
return musicInfos;
}
public static List<HashMap<String, String>> getMusicMaps(
List<MusicInfo> mp3Infos) {
List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
MusicInfo musicInfo = (MusicInfo) iterator.next();
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", musicInfo.getTitle());
map.put("Artist", musicInfo.getArtist());
map.put("album", musicInfo.getAlbum());
map.put("displayName", musicInfo.getDisplayName());
map.put("albumId", String.valueOf(musicInfo.getAlbumId()));
map.put("duration", formatTime(musicInfo.getDuration()));
map.put("size", String.valueOf(musicInfo.getSize()));
map.put("url", musicInfo.getUrl());
mp3list.add(map);
}
return mp3list;
}
/**
* 格式化时间,将毫秒转换为分:秒格式
* @param time
* @return
*/
public static String formatTime(long time) {
// TODO Auto-generated method stub
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);
}
}
package com.example.cungu.musicdemo;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MusicListAdapter extends BaseAdapter{
private Context context;
private List<MusicInfo> musicInfos;
private MusicInfo musicInfo;
/**
* 构造函数
* @param context 上下文
* @param mp3Infos 集合对象
*/
public MusicListAdapter(Context context, List<MusicInfo> mp3Infos) {
this.context = context;
this.musicInfos = mp3Infos;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return musicInfos.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null)
{
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list, null);
viewHolder.musicTitle = (TextView) convertView.findViewById(R.id.name);
viewHolder.musicDuration = (TextView) convertView.findViewById(R.id.time);
viewHolder.musicArtist = (TextView) convertView.findViewById(R.id.actor);
convertView.setTag(viewHolder); //表示给View添加一个格外的数据,
} else {
viewHolder = (ViewHolder)convertView.getTag();//通过getTag的方法将数据取出来
}
musicInfo = musicInfos.get(position);
viewHolder.musicTitle.setText(musicInfo.getTitle()); //显示标题
viewHolder.musicArtist.setText(musicInfo.getArtist()); //显示艺术家
viewHolder.musicDuration.setText(MusicUtil.formatTime(musicInfo.getDuration()));//显示时长
return convertView;
}
/**
* 定义一个内部类
* 声明相应的控件引用
*
*/
public class ViewHolder {
//所有控件对象引用
public ImageView albumImage; //专辑图片
public TextView musicTitle; //音乐标题
public TextView musicDuration; //音乐时长
public TextView musicArtist; //音乐艺术家
}
}
4、list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/actor"
android:layout_alignBottom="@+id/actor"
android:layout_alignParentRight="true"
android:layout_marginRight="28dp"
android:text="time"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_marginLeft="24dp"
android:text="name"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="@+id/actor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:textColor="#ffffff"
android:text="actor"
android:textSize="12sp"/>
RelativeLayout>
5、activity_player_list.xml主界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/music_play_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="25dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn_MusicList2"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:background="?attr/actionModeCloseDrawable"
android:backgroundTint="#ccffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="音乐列表"
android:layout_gravity="center_vertical"
android:textColor="#ffffff"
android:textSize="20sp" />
LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#afafaf"
android:layout_below="@+id/title"/>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
ListView>
LinearLayout>
PlayerListActivity.java:
package com.example.cungu.musicdemo;
import android.app.Activity;
import java.io.Serializable;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class PlayerListActivity extends Activity implements AdapterView.OnItemLongClickListener {
private ListView listview;
private List<MusicInfo> musicInfos = null;
private MusicListAdapter Listadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_list);
listview = (ListView) findViewById(R.id.list);
musicInfos = MusicUtil.getMp3Infos(PlayerListActivity.this);
Listadapter = new MusicListAdapter(this, musicInfos);
listview.setAdapter(Listadapter);
listview.setOnItemLongClickListener(this);
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent();
intent.putExtra("music", (Serializable) musicInfos);
intent.setClass(this, MainActivity.class);
startActivity(intent);
return false;
}
}
待解决问题:
如何把用适配器里的内容传给播放界面,播放!