Android第三次作业
1.实现的功能
实现播放,暂停,停止,播放上一首,下一首功能,至少可以播放3首歌曲
2.我的播放列表
3.关键代码
列表的实现:
public static List<String> list;
@Override
public void onCreate() {
super.onCreate();
list = new ArrayList<>();
new MyAsynctask().execute();
}
public class MyAsynctask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
scanFileList(Environment.getExternalStorageDirectory());
return null;
}
}
public void scanFileList(File parentFile) {
File[] listFile = parentFile.listFiles();
if (listFile != null) {
for (File file : listFile) {
if (file.isDirectory()) {
scanFileList(file);
} else if (file.isFile() && file.getName().endsWith(".mp3")) {
String path = file.getAbsolutePath();
list.add(path);
}
}
}
}
}
上一曲、下一曲的实现:
上一曲:
case R.id.bt_pre:
myBinder.callstop();
position--;
if (position < 0) {
position = MyApplication.list.size() - 1;
}
String prepath = MyApplication.list.get(position);
et_path.setText(prepath);
myBinder.callplay(prepath);
bt_play.setText("暂停");
isPlaying = true;
break;
下一曲:
case R.id.bt_next:
myBinder.callstop();
position++;
if (position > MyApplication.list.size() - 1) {
position = 0;
}
String nextpath = MyApplication.list.get(position);
et_path.setText(nextpath);
myBinder.callplay(nextpath);
bt_play.setText("暂停");
isPlaying = true;
break;
default:
break;
}
}
播放键、暂停键、停止键的实现:
播放键:
public void play() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
暂停键:
public void pause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
停止键:
public void stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
4.代码链接:https://git.dev.tencent.com/ronglu_/musicplayer.git
5.apk的下载地址:https://git.dev.tencent.com/ronglu_/apk.git