android第三次作业
界面:
主要代码:
1.定义一个工具类,在这个类中获取音频文件,并且对歌曲名、歌手和时间等进行格式规范:
package com.example.administrator.music;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import java.util.ArrayList;
import java.util.List;
/**
* Created by liuliu on 2018/1/14.
*/
//工具类
public class Utils {
//定义一个集合,存放从本地读取到的内容
public static List<Song> list;
public static Song song;
public static List<Song> getmusic(Context context) {
list = new ArrayList<>();
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
, null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC);
if (cursor != null) {
while (cursor.moveToNext()) {
song = new Song();
song.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
// 把歌曲名字和歌手切割开
if (song.size > 1000 * 800) {
if (song.song.contains("-")) {
String[] str = song.song.split("-");
song.singer = str[0];
song.song = str[1];
}
list.add(song);
}
}
}
cursor.close();
return list;
}
// 转换歌曲时间的格式
public static String formatTime(int time) {
if (time / 1000 % 60 < 10) {
String tt = time / 1000 / 60 + ":0" + time / 1000 % 60;
return tt;
} else {
String tt = time / 1000 / 60 + ":" + time / 1000 % 60;
return tt;
}
}
}
2.定义一个布局text.xml用于展示每一个音频文件的内容,使用适配器把把内容映射到主布局文件中的ListView中去:
class MyAdapter extends BaseAdapter {
Context context;
List<Song> list;
public MyAdapter(MainActivity mainActivity, List<Song> list) {
this.context = mainActivity;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Myholder myholder;
if (view == null) {
myholder = new Myholder();
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.text, null);
myholder.t_position = view.findViewById(R.id.t_postion);
myholder.t_song = view.findViewById(R.id.t_song);
myholder.t_singer = view.findViewById(R.id.t_singer);
myholder.t_duration = view.findViewById(R.id.t_duration);
view.setTag(myholder);
} else {
myholder = (Myholder) view.getTag();
}
myholder.t_song.setText(list.get(i).song.toString());
myholder.t_singer.setText(list.get(i).singer.toString());
String time = Utils.formatTime(list.get(i).duration);
myholder.t_duration.setText(time);
myholder.t_position.setText(i + 1 + "");
return view;
}
class Myholder {
TextView t_position, t_song, t_singer, t_duration;
}
}
3.定义播放play()、暂停pause()、停止stop()和循环播放loop()方法:
public void play(String path) {
try {
// 重置音频文件,防止多次点击会报错
mediaPlayer.reset();
// 调用方法传进播放地址
mediaPlayer.setDataSource(path);
// 异步准备资源,防止卡顿
mediaPlayer.prepareAsync();
// 调用音频的监听方法,音频准备完毕后响应该方法进行音乐播放
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
//设置循环播放。
loop();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
protected void play1() {
mediaPlayer.start();
//设置循环播放。
loop();
}
protected void pause() {
mediaPlayer.pause();
button2.setEnabled(true);
button2.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
protected void stop() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
button2.setEnabled(true);
Toast.makeText(this, "停止播放", Toast.LENGTH_LONG).show();
}
}
protected void loop(){
mediaPlayer.setLooping(true);
}
4.给按钮添加点击事件:
class myListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
//避免重复播放,把播放按钮设置为不可用
button2.setEnabled(false);
button2.setBackgroundColor(Color.GRAY);
//String p0 = list.get(current).path;//获得歌曲的地址
if(one<1)
{
String p = list.get(current).path;
play(p);
one++;
}
else {
play1();
}
break;
case R.id.button3:
pause();
break;
case R.id.button1:
if (current > 0) {
current = current - 1;
String p1 = list.get(current).path;//获得歌曲的地址
play(p1);
} else {
current = list.size() - 1;
String p1 = list.get(current).path;//获得歌曲的地址
play(p1);
}
break;
case R.id.button4:
if (current + 1 < list.size()) {
current = current + 1;
String p2 = list.get(current).path;//获得歌曲的地址
play(p2);
} else {
current = 0;
String p2 = list.get(current).path;//获得歌曲的地址
play(p2);
}
break;
case R.id.button5:
stop();
default:
break;
}
}
}
代码:https://git.coding.net/zwp598692456/androidmusic.git
app:https://coding.net/s/6d12c6ac-6efb-43ec-a2ab-f1332e3e5b57