android 簡易的播放器(實現了 播放、暫停、下一首和上一首)
最近專案中遇到一點小需求,就是有個本地音樂播放 ,功能簡單,實現了 播放、暫停、下一首和上一首,有基本需要的小夥伴可以參考(其他篇章寫過遍歷檔案)
1.首先是xml 佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_local_music"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentBottom="true">
<SeekBar
android:id="@+id/lv_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:progress="0" />
<TextView
android:id="@+id/tv_time_local"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/lv_seek_bar"
android:text="00:00" />
<TextView
android:id="@+id/tv_time_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00/"
android:layout_below="@+id/lv_seek_bar"
android:layout_toLeftOf="@+id/tv_time_local"
android:layout_toStartOf="@+id/tv_time_local" />
<TextView
android:id="@+id/tv_musicName"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/lv_seek_bar"
android:text="歌曲名稱"
android:singleLine="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#669fff"
android:orientation="horizontal">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/music" />
<ImageButton
android:id="@+id/ibtn_previous"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:background="@drawable/previous" />
<ImageButton
android:id="@+id/ibtn_play"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="@drawable/play" />
<ImageButton
android:id="@+id/ibtn_next"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="@drawable/next" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
2 .其次就是程式碼了(因為我們的不是全部使用手點選操作,所以只附上點選操作的程式碼)
public class MusicActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MusicActivity";
public static MusicActivity instance;
private MediaPlayer player;
private String musicName;
private ImageButton ibtn_play, ibtn_next, ibtn_previous;
private TextView tv_time_local, tv_musicName, tv_time_current;
private SeekBar seek_bar;
private ListView lv_local_music;
private List<UMusicBean> mMusicList;
private MusicListAdapter musicListAdapter;
private String speakText = "";
private SimpleDateFormat format = new SimpleDateFormat("mm:ss");
public static final int MUSIC_DURATION = 0X324;
public static final int MUSIC_POSITON = 0X325;
private int pPosition;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MUSIC_DURATION:
if (player != null) {
seek_bar.setMax(player.getDuration() / 1000);
String time = format.format(new Date(player.getDuration()));
tv_time_local.setText(time);
}
break;
case MUSIC_POSITON:
if (player != null) {
try {
seek_bar.setProgress(player.getCurrentPosition() / 1000);
tv_time_current.setText(format.format(new Date(player.getCurrentPosition())) + "/");
handler.sendEmptyMessageDelayed(MUSIC_POSITON, 1000);
} catch (Exception e) {
e.getStackTrace();
}
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_local);
EventBus.getDefault().register(this);
SpeechApp.addActivity(this);
instance = this;
SpeechApp.scene.add("91", "music", true, "1", true);
initview();
mMusicList = (List<UMusicBean>) getIntent().getSerializableExtra("mMusicList");
musicListAdapter = new MusicListAdapter(this, mMusicList);
lv_local_music.setAdapter(musicListAdapter);
lv_local_music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ibtn_play.setBackgroundResource(R.drawable.play); //更改按鍵狀態圖示
pPosition = position;
String musicPath = mMusicList.get(pPosition).getPath();
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
Log.i(TAG, "------musicPath------>" + musicPath);
startMusic(musicPath);
}
});
//進度條監聽
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
player.seekTo(progress * 1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
/**
* 初始化Ui
*/
private void initview() {
ibtn_play = (ImageButton) findViewById(R.id.ibtn_play);
ibtn_next = (ImageButton) findViewById(R.id.ibtn_next);
ibtn_previous = (ImageButton) findViewById(R.id.ibtn_previous);
lv_local_music = (ListView) findViewById(R.id.lv_local_music);
tv_time_local = (TextView) findViewById(R.id.tv_time_local);
tv_musicName = (TextView) findViewById(R.id.tv_musicName);
tv_time_current = (TextView) findViewById(R.id.tv_time_current);
seek_bar = (SeekBar) findViewById(R.id.lv_seek_bar);
ibtn_play.setOnClickListener(this);
ibtn_next.setOnClickListener(this);
ibtn_previous.setOnClickListener(this);
//建立mediaplayer
player = new MediaPlayer();
}
int totalNum;
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ibtn_play:
if (player.isPlaying()) { //如果正在播放則暫停
player.pause();
ibtn_play.setBackgroundResource(
R.drawable.pause); //更改按鍵狀態圖示
} else { //如果沒有播放則恢復播放
player.start();
ibtn_play.setBackgroundResource(
R.drawable.play); //更改按鍵狀態圖示
}
break;
case R.id.ibtn_next:
ibtn_play.setBackgroundResource(R.drawable.play); //更改按鍵狀態圖示
totalNum = mMusicList.size(); //獲得音樂的數目
if (pPosition == totalNum - 1) { //如果已經是最後一首,則播放第一首
pPosition = 0;
} else {
pPosition += 1; //否則播放下一首
}
String nextMusicPath = mMusicList.get(pPosition).getPath();//得到將要播放的音樂路徑
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
startMusic(nextMusicPath); //開始播放
break;
case R.id.ibtn_previous:
ibtn_play.setBackgroundResource(R.drawable.play); //更改按鍵狀態圖示
totalNum = mMusicList.size(); //獲得音樂的數目
if (pPosition == 0) { //如果已經時第一首則播放最後一首
pPosition = totalNum - 1;
} else { //否則播放上一首
pPosition -= 1;
}
String previousMusicPath = mMusicList.get(pPosition).getPath();//得到將要播放的音樂路徑
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
startMusic(previousMusicPath); //開始播放
break;
}
}
/**
* 播放音樂
*/
private void startMusic(String musicPath) {
//重置,第一次不需要重置
player.reset();
if (player.isPlaying()) {
player.release();
}
//設定播放的檔案的路徑
try {
player.setDataSource(musicPath);
Log.i("tag", "-------------" + musicName);
//設定準備完成的監聽器
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//播放
mp.start();
handler.sendEmptyMessage(MUSIC_DURATION);
handler.sendEmptyMessage(MUSIC_POSITON);
SpeechApp.speechHelper.nowIsAwaken(false);
}
});
//非同步載入
player.prepareAsync();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
int num = mMusicList.size(); //獲得音樂的數目
if (pPosition == num - 1) { //如果已經是最後一首,則播放第一首
pPosition = 0;
} else {
pPosition += 1; //否則播放下一首
}
String musicPath = mMusicList.get(pPosition).getPath();//得到將要播放的音樂路徑
tv_musicName.setText(mMusicList.get(pPosition).getDisplayName());
startMusic(musicPath); //開始播放
}
});
} catch (Exception e) {
e.printStackTrace();
}
return;
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
if (player.isPlaying()) {
player.release();
}
}
}
希望對大家有所幫助~