import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
private MediaPlayer mp;
private List<String> list = new ArrayList<String>();
private int location = 0;//当前播放歌曲的索引
private Button play;
private Button up;
private Button next;
private Button replay;
private boolean flag = false;
private Button stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.bt_play);
up = (Button) findViewById(R.id.bt_up);
next = (Button) findViewById(R.id.bt_next);
replay = (Button) findViewById(R.id.bt_replay);
stop = (Button) findViewById(R.id.bt_stop);
replay.setEnabled(false);
mp = new MediaPlayer();
showlist();
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (flag == false) {
playMusic(list.get(location));
play.setText("||");
flag = true;
replay.setEnabled(true);
} else {
yesOrNo();
}
}
});
up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
upMusic();
}
});
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextMusic();
}
});
replay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.seekTo(0);
mp.start();
play.setText("||");
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.stop();
flag = false;
play.setText("◎");
replay.setEnabled(false);
}
});
}
/*
*
* 显示音乐文件在ListView中
*/
private void showlist() {
getFile("/mnt/sdcard/qqmusic/import/");// 获取SD卡上的全部音乐文件
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);// 创建一个适配器
ListView lv_list = (ListView) findViewById(R.id.lv_list);
lv_list.setAdapter(adapter);
lv_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
location = arg2;
playMusic(list.get(location));
}
});
}
/*
* 获取音乐文件
*/
public void getFile(String url) {
File files = new File(url);
File[] file = files.listFiles();
for (File f : file) {
if (f.isDirectory()) { // 如果是文件夹--(此处存在问题,地址写全代码不过此处可以正常运行,否则null,需改进)
getFile(f.getAbsolutePath()); // 递归调用--(此处存在问题,地址写全代码不过此处可以正常运行,否则null,需改进)
} else {
if (isAudioFile(f.getPath())) { // 如果是规定文件格式
list.add(f.getPath()); // 将文件添加到集合中
}
}
}
}
/*
*
* 判断文件格式
*/
private boolean isAudioFile(String path) {
if (path.contains("mp3")) {
return true;
}
return false;
}
/*
*
* 播放
*/
public void playMusic(String path) {
if (mp.isPlaying()) {
mp.stop();
}
mp.reset();// 重置MediaPlayer
try {
mp.setDataSource(path);
mp.prepare();
mp.start();
play.setText("||");
flag = true;
replay.setEnabled(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
*
* 暂停、继续
*/
public void yesOrNo() {
if (mp != null && mp.isPlaying()) {
mp.pause();
play.setText("◎");
} else {
mp.start();
play.setText("||");
}
}
/*
*
* 下一首
*/
public void nextMusic() {
if (++location > list.size()) {
location = 0;
}
play.setText("||");
flag = true;
replay.setEnabled(true);
playMusic(list.get(location));
}
/*
*
* 上一首
*/
public void upMusic() {
if (--location < 0) {
location = list.size() - 1;
}
play.setText("||");
flag = true;
replay.setEnabled(true);
playMusic(list.get(location));
}
@Override
protected void onDestroy() {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
super.onDestroy();
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。