Android--MediaPlayer(实现列表选歌,上一首,下一首,清空播放列表,搜索本地音乐文件)
MediaPlayerActivity类:
[html]
<span style="font-size:14px;">package com.vince.media;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MediaPlayerActivity extends Activity implements OnCompletionListener,OnErrorListener,OnSeekBarChangeListener,OnItemClickListener,Runnable{
protected static final int SEARCH_MUSIC_SUCCESS = 0;// 搜索成功标记
private SeekBar seekBar;
private ListView listView;
private ImageButton btnPlay;
private TextView tv_currTime,tv_totalTime,tv_showName;
private List<String> list;
private ProgressDialog pd; // 进度条对话框
private MusicListAdapter ma;// 适配器
private MediaPlayer mp;
private int currIndex = 0;// 表示当前播放的音乐索引
private boolean flag = true;//控制进度条线程标记
// 定义当前播放器的状态״̬
private static final int IDLE = 0;
private static final int PAUSE = 1;
private static final int START = 2;
private static final int CURR_TIME_VALUE = 1;
private int currState = IDLE; // 当前播放器的状态
//定义线程池(同时只能有一个线程运行)
ExecutorService es = Executors.newSingleThreadExecutor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mediaplayer_layout);
list = new ArrayList<String>();
mp = new MediaPlayer();
mp.setOnCompletionListener(this);
mp.setOnErrorListener(this);
initView();
}
@Override
protected void onDestroy() {
if (mp != null) {
mp.stop();
flag= false;
//释放资源
mp.release();
}
super.onDestroy();
}
/**
* 初始化UI组件
*/
private void initView() {
btnPlay = (ImageButton) findViewById(R.id.media_play);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener(this);
listView = (ListView) findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
tv_currTime = (TextView) findViewById(R.id.textView1_curr_time);
tv_totalTime = (TextView) findViewById(R.id.textView1_total_time);
tv_showName = (TextView) findViewById(R.id.tv_showName);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//从xml文件中装载菜单
getMenuInflater().inflate(R.menu.media_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private Handler hander = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SEARCH_MUSIC_SUCCESS:
//搜索音乐文件结束时
ma = new MusicListAdapter();
listView.setAdapter(ma);
pd.dismiss();
break;
case CURR_TIME_VALUE:
//设置当前时间
tv_currTime.setText(msg.obj.toString());
break;
default:
break;
}
};
};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//搜索本地音乐
补充:移动开发 , Android ,