Android應用開發 MP3音樂播放器程式碼實現 三
阿新 • • 發佈:2018-11-08
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Android應用開發-MP3音樂播放器程式碼實現(三)
2013年5月29 簡、美音樂播放器開發記錄
這篇部落格是接著上一篇部落格的,點選列表會進入播放的Activity,在這個Activity會接收到從前面的Activity傳來的資料,在通過這些資料來啟動服務來播放音樂,整個過程很簡單,需要注意的是我們要接收的資料是哪些內容,還有我們向Service要傳什麼樣的資料,這是根據自己的開發思路和需求來決定的,等一下你們會看到小巫到底往Service傳了些什麼東西,然而這些資料到底起什麼作用,需要慢慢去體會,因為這是需要一點一點去除錯的。
好了,下面貼一下整個Activity的實現程式碼,主要是播放的各種狀態的實現,因為這個音樂播放器並沒有完全開發完,所以朋友們需要弄清楚這一點。如果對程式碼的實現有不清楚的,可以給小巫留言,小巫有空一定給大家解答。
PlayerActivity.java程式碼:
package com.wwj.sb.activity;import java.util.List;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;import android.widget.Toast;import com.wwj.sb.domain.AppConstant;import com.wwj.sb.domain.Mp3Info;import com.wwj.sb.utils.MediaUtil;/** * 播放音樂介面 * @author wwj * 從主介面傳遞過來歌曲的Id、歌曲名、歌手、歌曲路徑、播放狀態 */public class PlayerActivity extends Activity{ private TextView musicTitle = null; private TextView musicArtist = null; private Button previousBtn; // 上一首 private Button repeatBtn; // 重複(單曲迴圈、全部迴圈) private Button playBtn; // 播放(播放、暫停) private Button shuffleBtn; // 隨機播放 private Button nextBtn; // 下一首 private Button searchBtn; //查詢歌曲 private Button queueBtn; //歌曲列表 private SeekBar music_progressBar; //歌曲進度 private TextView currentProgress; //當前進度消耗的時間 private TextView finalProgress; //歌曲時間 private String title; //歌曲標題 private String artist; //歌曲藝術家 private String url; //歌曲路徑 private int listPosition; //播放歌曲在mp3Infos的位置 private int currentTime; //當前歌曲播放時間 private int duration; //歌曲長度 private int flag; //播放標識 private int repeatState; private final int isCurrentRepeat = 1; // 單曲迴圈 private final int isAllRepeat = 2; // 全部迴圈 private final int isNoneRepeat = 3; // 無重複播放 private boolean isPlaying; // 正在播放 private boolean isPause; // 暫停 private boolean isNoneShuffle; // 順序播放 private boolean isShuffle; // 隨機播放 private List<Mp3Info> mp3Infos; private PlayerReceiver playerReceiver; public static final String UPDATE_ACTION = "com.wwj.action.UPDATE_ACTION"; //更新動作 public static final String CTL_ACTION = "com.wwj.action.CTL_ACTION"; //控制動作 public static final String MUSIC_CURRENT = "com.wwj.action.MUSIC_CURRENT"; //音樂當前時間改變動作 public static final String MUSIC_DURATION = "com.wwj.action.MUSIC_DURATION";//音樂播放長度改變動作 public static final String MUSIC_PLAYING = "com.wwj.action.MUSIC_PLAYING"; //音樂正在播放動作 public static final String REPEAT_ACTION = "com.wwj.action.REPEAT_ACTION"; //音樂重複播放動作 public static final String SHUFFLE_ACTION = "com.wwj.action.SHUFFLE_ACTION";//音樂隨機播放動作 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.play_activity_layout); musicTitle = (TextView) findViewById(R.id.musicTitle); musicArtist = (TextView) findViewById(R.id.musicArtist); findViewById(); setViewOnclickListener(); mp3Infos = MediaUtil.getMp3Infos(PlayerActivity.this); playerReceiver = new PlayerReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(UPDATE_ACTION); filter.addAction(MUSIC_CURRENT); filter.addAction(MUSIC_DURATION); registerReceiver(playerReceiver, filter); } /** * 從介面上根據id獲取按鈕 */ private void findViewById() { previousBtn = (Button) findViewById(R.id.previous_music); repeatBtn = (Button) findViewById(R.id.repeat_music); playBtn = (Button) findViewById(R.id.play_music); shuffleBtn = (Button) findViewById(R.id.shuffle_music); nextBtn = (Button) findViewById(R.id.next_music); searchBtn = (Button) findViewById(R.id.search_music); queueBtn = (Button) findViewById(R.id.play_queue); music_progressBar = (SeekBar) findViewById(R.id.audioTrack); currentProgress = (TextView) findViewById(R.id.current_progress); finalProgress = (TextView) findViewById(R.id.final_progress); } /** * 給每一個按鈕設定監聽器 */ private void setViewOnclickListener() { ViewOnclickListener ViewOnClickListener = new ViewOnclickListener(); previousBtn.setOnClickListener(ViewOnClickListener); repeatBtn.setOnClickListener(ViewOnClickListener); playBtn.setOnClickListener(ViewOnClickListener); shuffleBtn.setOnClickListener(ViewOnClickListener); nextBtn.setOnClickListener(ViewOnClickListener); searchBtn.setOnClickListener(ViewOnClickListener); queueBtn.setOnClickListener(ViewOnClickListener); music_progressBar.setOnSeekBarChangeListener(new SeekBarChangeListener()); } /** * 在OnResume中初始化和接收Activity資料 */ @Override protected void onResume() { super.onResume(); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); title = bundle.getString("title"); artist = bundle.getString("artist"); url = bundle.getString("url"); listPosition = bundle.getInt("listPosition"); repeatState = bundle.getInt("repeatState"); isShuffle = bundle.getBoolean("shuffleState"); flag = bundle.getInt("MSG"); currentTime = bundle.getInt("currentTime"); duration = bundle.getInt("duration"); initView(); } /** * 初始化介面 */ public void initView() { musicTitle.setText(title); musicArtist.setText(artist); music_progressBar.setProgress(currentTime); music_progressBar.setMax(duration); switch (repeatState) { case isCurrentRepeat: // 單曲迴圈 shuffleBtn.setClickable(false); repeatBtn.setBackgroundResource(R.drawable.repeat_current_selector); break; case isAllRepeat: // 全部迴圈 shuffleBtn.setClickable(false); repeatBtn.setBackgroundResource(R.drawable.repeat_all_selector); break; case isNoneRepeat: // 無重複 shuffleBtn.setClickable(true); repeatBtn.setBackgroundResource(R.drawable.repeat_none_selector); break; } if(isShuffle) { isNoneShuffle = false; shuffleBtn.setBackgroundResource(R.drawable.shuffle_selector); repeatBtn.setClickable(false); } else { isNoneShuffle = true; shuffleBtn.setBackgroundResource(R.drawable.shuffle_none_selector); repeatBtn.setClickable(true); } if(flag == AppConstant.PlayerMsg.PLAYING_MSG) { //如果播放資訊是正在播放 Toast.makeText(PlayerActivity.this, "正在播放--" + title, 1).show(); } else if(flag == AppConstant.PlayerMsg.PLAY_MSG) { //如果是點選列表播放歌曲的話 play(); } playBtn.setBackgroundResource(R.drawable.play_selector); isPlaying = true; isPause = false; } /** * 反註冊廣播 */ @Override protected void onStop() { super.onStop(); unregisterReceiver(playerReceiver); } @Override protected void onDestroy() { super.onDestroy(); } /** * 控制元件點選事件 * @author wwj * */ private class ViewOnclickListener implements OnClickListener { Intent intent = new Intent(); @Override public void onClick(View v) { switch(v.getId()) { case R.id.play_music: if (isPlaying) { playBtn.setBackgroundResource(R.drawable.pause_selector); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG); startService(intent); isPlaying = false; isPause = true; } else if (isPause) { playBtn.setBackgroundResource(R.drawable.play_selector); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("MSG", AppConstant.PlayerMsg.CONTINUE_MSG); startService(intent); isPause = false; isPlaying = true; } break; case R.id.previous_music: //上一首歌曲 previous_music(); break; case R.id.next_music: //下一首歌曲 next_music(); break; case R.id.repeat_music: //重複播放音樂 if (repeatState == isNoneRepeat) { repeat_one(); shuffleBtn.setClickable(false); //是隨機播放變為不可點選狀態 repeatState = isCurrentRepeat; } else if (repeatState == isCurrentRepeat) { repeat_all(); shuffleBtn.setClickable(false); repeatState = isAllRepeat; } else if (repeatState == isAllRepeat) { repeat_none(); shuffleBtn.setClickable(true); repeatState = isNoneRepeat; } Intent intent = new Intent(REPEAT_ACTION); switch (repeatState) { case isCurrentRepeat: // 單曲迴圈 repeatBtn .setBackgroundResource(R.drawable.repeat_current_selector); Toast.makeText(PlayerActivity.this, R.string.repeat_current, Toast.LENGTH_SHORT).show(); intent.putExtra("repeatState", isCurrentRepeat); sendBroadcast(intent); break; case isAllRepeat: // 全部迴圈 repeatBtn .setBackgroundResource(R.drawable.repeat_all_selector); Toast.makeText(PlayerActivity.this, R.string.repeat_all, Toast.LENGTH_SHORT).show(); intent.putExtra("repeatState", isAllRepeat); sendBroadcast(intent); break; case isNoneRepeat: // 無重複 repeatBtn .setBackgroundResource(R.drawable.repeat_none_selector); Toast.makeText(PlayerActivity.this, R.string.repeat_none, Toast.LENGTH_SHORT).show(); intent.putExtra("repeatState", isNoneRepeat); break; } break; case R.id.shuffle_music: //隨機播放狀態 Intent shuffleIntent = new Intent(SHUFFLE_ACTION); if (isNoneShuffle) { //如果當前狀態為非隨機播放,點選按鈕之後改變狀態為隨機播放 shuffleBtn .setBackgroundResource(R.drawable.shuffle_selector); Toast.makeText(PlayerActivity.this, R.string.shuffle, Toast.LENGTH_SHORT).show(); isNoneShuffle = false; isShuffle = true; shuffleMusic(); repeatBtn.setClickable(false); shuffleIntent.putExtra("shuffleState", true); sendBroadcast(shuffleIntent); } else if (isShuffle) { shuffleBtn .setBackgroundResource(R.drawable.shuffle_none_selector); Toast.makeText(PlayerActivity.this, R.string.shuffle_none, Toast.LENGTH_SHORT).show(); isShuffle = false; isNoneShuffle = true; repeatBtn.setClickable(true); shuffleIntent.putExtra("shuffleState", false); sendBroadcast(shuffleIntent); } break; } } } /** * 實現監聽Seekbar的類 * @author wwj * */ private class SeekBarChangeListener implements OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if(fromUser) { audioTrackChange(progress); //使用者控制進度的改變 } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } } /** * 播放音樂 */ public void play() { //開始播放的時候為順序播放 repeat_none(); Intent intent = new Intent(); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("url", url); intent.putExtra("listPosition", listPosition); intent.putExtra("MSG", flag); startService(intent); } /** * 隨機播放 */ public void shuffleMusic() { Intent intent = new Intent(CTL_ACTION); intent.putExtra("control", 4); sendBroadcast(intent); } public void audioTrackChange(int progress) { Intent intent = new Intent(); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("url", url); intent.putExtra("listPosition", listPosition); if(isPause) { intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG); } else { intent.putExtra("MSG", AppConstant.PlayerMsg.PROGRESS_CHANGE); } intent.putExtra("progress", progress); startService(intent); } /** * 單曲迴圈 */ public void repeat_one() { Intent intent = new Intent(CTL_ACTION); intent.putExtra("control", 1); sendBroadcast(intent); } /** * 全部迴圈 */ public void repeat_all() { Intent intent = new Intent(CTL_ACTION); intent.putExtra("control", 2); sendBroadcast(intent); } /** * 順序播放 */ public void repeat_none() { Intent intent = new Intent(CTL_ACTION); intent.putExtra("control", 3); sendBroadcast(intent); } /** * 上一首 */ public void previous_music() { playBtn.setBackgroundResource(R.drawable.play_selector); listPosition = listPosition - 1; if(listPosition >= 0) { Mp3Info mp3Info = mp3Infos.get(listPosition); //上一首MP3 musicTitle.setText(mp3Info.getTitle()); musicArtist.setText(mp3Info.getArtist()); url = mp3Info.getUrl(); Intent intent = new Intent(); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("url", mp3Info.getUrl()); intent.putExtra("listPosition", listPosition); intent.putExtra("MSG", AppConstant.PlayerMsg.PRIVIOUS_MSG); startService(intent); } else { Toast.makeText(PlayerActivity.this, "沒有上一首了", Toast.LENGTH_SHORT).show(); } } /** * 下一首 */ public void next_music() { playBtn.setBackgroundResource(R.drawable.play_selector); listPosition = listPosition + 1; if(listPosition <= mp3Infos.size() - 1) { Mp3Info mp3Info = mp3Infos.get(listPosition); url = mp3Info.getUrl(); musicTitle.setText(mp3Info.getTitle()); musicArtist.setText(mp3Info.getArtist()); Intent intent = new Intent(); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("url", mp3Info.getUrl()); intent.putExtra("listPosition", listPosition); intent.putExtra("MSG", AppConstant.PlayerMsg.NEXT_MSG); startService(intent); } else { Toast.makeText(PlayerActivity.this, "沒有下一首了", Toast.LENGTH_SHORT).show(); } } /** * 用來接收從service傳回來的廣播的內部類 * @author wwj * */ public class PlayerReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(MUSIC_CURRENT)) { currentTime = intent.getIntExtra("currentTime", -1); currentProgress.setText(MediaUtil.formatTime(currentTime)); music_progressBar.setProgress(currentTime); } else if(action.equals(MUSIC_DURATION)) { int duration = intent.getIntExtra("duration", -1); music_progressBar.setMax(duration); finalProgress.setText(MediaUtil.formatTime(duration)); } else if(action.equals(UPDATE_ACTION)) { //獲取Intent中的current訊息,current代表當前正在播放的歌曲 listPosition = intent.getIntExtra("current", -1); url = mp3Infos.get(listPosition).getUrl(); if(listPosition >= 0) { musicTitle.setText(mp3Infos.get(listPosition).getTitle()); musicArtist.setText(mp3Infos.get(listPosition).getArtist()); } if(listPosition == 0) { finalProgress.setText(MediaUtil.formatTime(mp3Infos.get(listPosition).getDuration())); playBtn.setBackgroundResource(R.drawable.pause_selector); isPause = true; } } } } }
以上程式碼主要有以下幾點實現:
1. 播放狀態(上一首、下一首、暫停音樂、播放音樂、重複播放、隨機播放)
2. 進度更新(自定義Seekbar,Seekbar觸發時間控制音樂播放的位置)
3. 接收來自Service的廣播,對播放時間,歌曲資訊的UI更新。
要注意的地方:
1. 在onResume裡接收來自HomeActivity裡通過Intent傳過來的資料,它是儲存在bundle當中的,所以可以通過Bundle來取資料。
2. 通過startSercice來啟動服務,在啟動之前我們往服務傳的資料
/** * 播放音樂 */ public void play() { //開始播放的時候為順序播放 repeat_none(); Intent intent = new Intent(); intent.setAction("com.wwj.media.MUSIC_SERVICE"); intent.putExtra("url", url); intent.putExtra("listPosition", listPosition); intent.putExtra("MSG", flag); startService(intent); }
可以看到只有url(音樂播放路徑)、listPosition(列表的點選位置,也就是mp3Infos的位置)、MSG(代表播放資訊)。這幾個是來控制播放的,在Service會很清楚看到這幾個引數的作用。這會在下一篇部落格中介紹。