/**
* MediaPlayer的两种创建方式 1.静态的 raw
* 播放音乐–>硬件,java 调用不了硬件,C C++ c#
* @author Administrator
*
*/
public class MainActivity extends Activity implements OnClickListener {
// 文件路径
private String path1 = “/sdcard/ylgz.mp3”;
private String path2 = “/sdcard/qqqg.mp3”;
private MediaPlayer mediaPlayer;
private boolean isPuase = true;
private Button pause;
private SeekBar seekBar;
private Timer timer;
private ArrayList mp3_List = new ArrayList();
private ArrayList pathList;
private int current_num=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化界面
initViews();
pathList = new ArrayList();
pathList.add(path1);
pathList.add(path2);
}
private void initViews() {
Button play = (Button) findViewById(R.id.play);
Button stop = (Button) findViewById(R.id.stop);
pause = (Button) findViewById(R.id.pause);
seekBar = (SeekBar) findViewById(R.id.seek);
Button next=(Button) findViewById(R.id.next);
Button previous=(Button) findViewById(R.id.previous);
// 设置拖动监听
seekBar.setOnSeekBarChangeListener(new MySeekBar());
play.setOnClickListener(this);
stop.setOnClickListener(this);
pause.setOnClickListener(this);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
// 拖动监听
class MySeekBar implements OnSeekBarChangeListener {
// onProgressChanged 当进度改变时调用
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
// 开始拖动的时候调用
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
// 当停止拖动的时候调用
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 获取seekBar 的位置
int progress = seekBar.getProgress();
// 设置播放的进度
mediaPlayer.seekTo(progress);
}
}
//所有的点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
// 播放
case R.id.play:
Toast.makeText(MainActivity.this, “播放”, 0).show();
mediaPlayer = new MediaPlayer();
// 1.重置
mediaPlayer.reset();
// 2.设置资源
try {
mediaPlayer.setDataSource(pathList.get(current_num));
// 设置seekbar的总长度
// 3.准备
mediaPlayer.prepare();
// 4,开始播放
mediaPlayer.start();
int duration = mediaPlayer.getDuration();// 获取总时长
seekBar.setMax(duration);
timer = new Timer();
timer.schedule(new MyTask(), 0, 1000);
} catch (Exception e) {
e.printStackTrace();
}
break;
// 停止
case R.id.stop:
// 停止播放
if (mediaPlayer != null) {
mediaPlayer.stop();
}
break;
// 暂停和继续播放
case R.id.pause:
if (isPuase) {
mediaPlayer.pause();
pause.setText(“继续播放”);
} else {
mediaPlayer.start();
}
isPuase = !isPuase;
break;
//下一首
case R.id.next:
current_num++;
if (current_num<=pathList.size()-1) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(pathList.get(current_num));
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
current_num=pathList.size()-1;
Toast.makeText(MainActivity.this, “已经是最后一首了”, 0).show();
}
break;
case R.id.previous:
current_num--;
if (current_num<0) {
current_num=0;
Toast.makeText(MainActivity.this, "已经是第一一首了", 0).show();
}else{
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(pathList.get(current_num));
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
break;
}
}
// 计时器任务栈
class MyTask extends TimerTask {
@Override
public void run() {
Log.e("Main-->", "获取进度");
int currentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentPosition);
}
}
//搜秒SDcard mp3文件
public ArrayList<String> getMp3Files(File file){
//获取Sdcard路径 Environment环境
//递归
if (file.isDirectory()) {
//获取文件数组
File[] files = file.listFiles();
if (files.length>0) {
//遍历
for (File file2 : files) {
if (file2.isDirectory()) {
//递归
getMp3Files(file2);
}
else{
if (file2.getName().endsWith(".mp3")) {
mp3_List.add(file2.getName());
}
}
}
}
}
return mp3_List;
}
}