1、寻找控件
2.扫描sdcard下的map3文件
3.将扫描的文件添加到集合list中,并展示出来
setAdapter();
4.为listview设置条目点击监听事件(开启音乐)
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
cu = position;
//关闭timer任务(实时获取歌曲的进度)
stopTimer();
// 调用开启音乐的方法
startMusic(position);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
5、开始音乐的方法
private void startMusic(int position) throws IOException {
// 关闭正在播放的歌曲(避免播放多首歌曲)
if (player != null && player.isPlaying()) {
player.stop();
player.release();
player = null;
}
// 播放选中的歌曲
player = new MediaPlayer();
player.setDataSource(list.get(position).getPath());
player.prepare();
player.start();
//获取当前歌曲的总长度
int duration = player.getDuration();
//将歌曲 的总长度赋值给seekbar
seekbar.setMax(duration);
//使seekbar实时获取歌曲的进度
getProgress();
}
6、实时获取歌曲的进度
private void getProgress() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//获取歌曲的进度
int p = player.getCurrentPosition();
//将获取歌曲的进度赋值给seekbar
seekbar.setProgress(p);
}
}, 0, 2000);
}
7、实现拖拽拖动条,歌曲可以以拖到的点接着播放歌曲(seekbar的监听事件)
//监听seekbar的进度
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取进度条的进度
int p = seekBar.getProgress();
//将进度条的进度赋值给歌曲
player.seekTo(p);
//开始音乐继续获取歌曲的进度
getProgress();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//取消timer任务
stopTimer();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
}
8、从本activity跳转到另一个activity,然后再返回来,歌曲再继续播放
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("main----onStop");
stopTimer();
// 记录当前歌曲的进度
currentPosition = player.getCurrentPosition();
SharedPreferences share = getSharedPreferences("current", 0);
share.edit().putInt("id", currentPosition).putInt("nameid", cu)
.commit();
// 暂停歌曲
player.stop();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("main----onRestart");
try {
// 重新开始歌曲
SharedPreferences share = getSharedPreferences("current", 0);
int i = share.getInt("id", 0);
player.prepare();
player.seekTo(i);
player.start();
//seekbar实时获取歌曲进度
getProgress();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}/*
* catch (IOException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
9.//关闭timer
public void stopTimer()
{
if(timer!=null)
{
timer.cancel();
}
}