public class MainActivity extends AppCompatActivity implements View.OnClickListener { //音乐文件的目录 private static final String PATH = Environment.getExternalStorageDirectory() + "/Music/"; private static final String TAG = "MainActivity"; //文件路径 private ArrayList<String> fileList = new ArrayList<String>(); //文件名 private ArrayList<String> fileNameList = new ArrayList<String>();
/*
进度条
*/
private SeekBar seekBar;
/*
音乐文件列表
*/
private Button musicList;
/*
音乐控制按键
*/
private ImageButton stop;
private ImageButton pre;
private ImageButton play;
private ImageButton next;
//单首音乐的路径
private String musicPath;
private IMusicPlayerService mPlayerService;
private MusicPlayerServiceConnection mConn;
private boolean mBound =false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
初始化
*/
seekBar = (SeekBar) findViewById(R.id.seekBar);
musicList = (Button) findViewById(R.id.musicList);
stop = (ImageButton) findViewById(R.id.stop);
pre = (ImageButton) findViewById(R.id.pre);
;
play = (ImageButton) findViewById(R.id.play);
;
next = (ImageButton) findViewById(R.id.next);
;
File file = new File(PATH);
//获取文件列表
File[] arrs = file.listFiles(new MusicFileNameFilter(".mp3"));
for (File f : arrs) {
//添加全路径到文件列表
fileList.add(f.getAbsolutePath());
//添加文件表到文件列表
fileNameList.add(f.getName());
}
/*
设置监听器
*/
musicList.setOnClickListener(this);
stop.setOnClickListener(this);
pre.setOnClickListener(this);
play.setOnClickListener(this);
next.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
//拖动进度条,改变播放进度
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
mPlayerService.callChanageSeek(seekBar.getProgress());
}
});
}
@Override
protected void onStart() {
super.onStart();
if(mConn==null){
mConn= new MusicPlayerServiceConnection();
}
Intent intent = new Intent(this,MusicPlayerService.class);
mBound = bindService(intent, mConn, BIND_AUTO_CREATE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.musicList:
showList();
break;
case R.id.stop:
stop();
break;
case R.id.pre:
pre();
break;
case R.id.play:
play();
break;
case R.id.next:
next();
break;
}
}
/*
下一首
*/
private void next() {
int index = fileList.indexOf(musicPath);
if(index>=fileList.size()){
index=0;
}
mPlayerService.callplay(fileList.get(index+1));
}
/*
播放
*/
private void play() {
Log.d(TAG, "mBound" + mBound);
boolean isNull = mPlayerService.callMediaIsNull();
if(isNull) {
mPlayerService.callplay(musicPath);
}else{
mPlayerService.callPause();
}
if(mPlayerService.callIsPlaying()){
play.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.pause));
}else{
play.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.play));
}
//更新进度
new Thread(){
boolean isFinished=mPlayerService.callIsPlaying();
@Override
public void run() {
if(isFinished) {
while (isFinished) {
SystemClock.sleep(200);
int currentDuration = mPlayerService.callGetgetCurrentDuration();
int duration = mPlayerService.callGetgetDuration();
seekBar.setMax(duration);
seekBar.setProgress(currentDuration);
if (currentDuration >= duration) {
isFinished = false;
}
}
}
}
}.start();
}
/**
* 上一首
*/
private void pre() {
int index = fileList.indexOf(musicPath);
if(index<=0){
index=fileList.size()-1;
}
mPlayerService.callplay(fileList.get(index-1));
}
/**
* 停止播放
*/
private void stop() {
mPlayerService.callStop();
}
/**
* 显示音乐列表
*/
private void showList() {
Intent intent = new Intent(this,MusicListActivity.class);
//intent.putStringArrayListExtra("filelist",fileList);
intent.putStringArrayListExtra("filenamelist",fileNameList);
startActivityForResult(intent,100);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode,
data);
if(data==null){
Toast.makeText(MainActivity.this, "没有结果",
Toast.LENGTH_SHORT).show();
return;
}
//获取下标
int position = data.getIntExtra("position",
0);
//设置音乐路径
musicPath = fileList.get(position);
// play();
Log.d(TAG,musicPath);
}
private class MusicPlayerServiceConnection implements
ServiceConnection{
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mPlayerService = (IMusicPlayerService) service;
}
@Override
public void onServiceDisconnected(ComponentName
name) {
if(mConn!=null){
mConn =null;
}
}
}
@Override
protected void onStop() {
super.onStop();
if(mConn!=null){
unbindService(mConn);
mConn=null;
mPlayerService=null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mConn!=null){
unbindService(mConn);
mConn=null;
mPlayerService=null;
}
}
} |