代码太长。。给出部分代码,具体的 看附件 ,可以直接导入运行
/**
* 上一首
*/
public void pree(){
int n_index = currIndex;
n_index--;
if(filelist == null){
return;
}
if (n_index < 0) {
n_index = filelist.length - 1;
}
currIndex = n_index;
play(filelist[n_index].getPath());
}
/**
* 下一首
* @param path
*/
public void next(){
int n_index = currIndex;
n_index++;
if(filelist == null){
return;
}
if (n_index >= filelist.length) {
n_index = 0;
}
n_index = (int) (System.currentTimeMillis() % filelist.length);
currIndex = n_index;
play(filelist[n_index].getPath());
}
public void setLiveView(String path) {
filelist = getList(path);
String[] data = new String[filelist.length];
for (int i = 0; i <= filelist.length - 1; i++) {
data[i] = filelist[i].getPath();
}
// 绑定ListView和ArrayAdapter
lv_list.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, data));
// 添加点击
lv_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View v, int arg2,
long arg3) {
setTitle(filelist[arg2].getName());
currIndex = arg2;
play(filelist[arg2].getPath());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (FILE_RESULT_CODE == requestCode) {
Bundle bundle = null;
if (data != null && (bundle = data.getExtras()) != null) {
// bundle.getString("file");
// setTitle(bundle.getString("file"));
setLiveView(bundle.getString("file"));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public File[] getList(String filepath) {
File file = new File(filepath);
File[] filelist = file.listFiles();
return filelist;
}
public void play(String filepath) {
mediaPlayer.reset();
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();// 重置为初始状态
}
try {
mediaPlayer.setDataSource(filepath);// "/storage/extSdCard/My Music"
} 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();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// mediaPlayer.release();
mediaPlayer.start();// 开始或恢复播放
bt_zan.setText("开始");
isstop = false;
// mediaPlayer.pause();//暂停播放
// mediaPlayer.start();//恢复播放
// mediaPlayer.stop();//停止播放
// mediaPlayer.release();//释放资源
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {// 播出完毕事件
@Override
public void onCompletion(MediaPlayer arg0) {
// mediaPlayer.release();
if(mode == 1){ // 顺序播放
next();
}
else if(mode == 2){ //随机播放
// double ran = Math.random();// filelist.length;
// ran = ran * filelist.length;
// int ran = Time.SECOND % filelist.length;
next();
}
else{
next();
}
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {// 错误处理事件
@Override
public boolean onError(MediaPlayer player, int arg1,
int arg2) {
mediaPlayer.release();
return false;
}
});
}