方法:stop()
解釋:無返回值,停止播放
4、完整音樂播放例子演示
實現的功能:
1.暫停/播放、下一首/上一首,點擊某一首時播放
2.支持拖動進度條快進
3.列表排序
4.來電話時,停止播放,掛斷後繼續播放
5.可在後台播放
界面:
main.xml:
item.xml:
MainActivity:
import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import Android.app.Activity; import Android.content.BroadcastReceiver; import Android.content.Context; import Android.content.Intent; import Android.content.IntentFilter; import Android.media.MediaPlayer; import Android.media.MediaPlayer.OnCompletionListener; import Android.os.Bundle; import Android.os.Environment; import Android.os.Handler; import Android.telephony.PhoneStateListener; import Android.telephony.TelephonyManager; import Android.view.View; import Android.widget.AdapterView; import Android.widget.Button; import Android.widget.ListView; import Android.widget.SeekBar; import Android.widget.SimpleAdapter; import Android.widget.TextView; import Android.widget.AdapterView.OnItemClickListener; import Android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity { private TextView nameTextView; private SeekBar seekBar; private ListView listView; private List> data; private int current; private MediaPlayer player; private Handler handler = new Handler(); private Button ppButton; private boolean isPause; private boolean isStartTrackingTouch; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameTextView = (TextView) findViewById(R.id.name); seekBar = (SeekBar) findViewById(R.id.seekBar); listView = (ListView) findViewById(R.id.list); ppButton = (Button) findViewById(R.id.pp); //創建一個音樂播放器 player = new MediaPlayer(); //顯示音樂播放列表 generateListView(); //進度條監聽器 seekBar.setOnSeekBarChangeListener(new MySeekBarListener()); //播放器監聽器 player.setOnCompletionListener(new MyPlayerListener()); //意圖過濾器 IntentFilter filter = new IntentFilter(); //播出電話暫停音樂播放 filter.addAction("Android.intent.action.NEW_OUTGOING_CALL"); registerReceiver(new PhoneListener(), filter); //創建一個電話服務 TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); //監聽電話狀態,接電話時停止播放 manager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE); } private final class MyPhoneStateListener extends PhoneStateListener { public void onCallStateChanged(int state, String incomingNumber) { pause(); } } private final class MyPlayerListener implements OnCompletionListener { //歌曲播放完後自動播放下一首歌曲 public void onCompletion(MediaPlayer mp) { next(); } } public void next(View view) { next(); } public void previous(View view) { previous(); } private void previous() { current = current - 1 < 0 ? data.size() - 1 : current - 1; play(); } private void next() { current = (current + 1) % data.size(); play(); } private final class MySeekBarListener implements OnSeekBarChangeListener { //移動觸發 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } //起始觸發 public void onStartTrackingTouch(SeekBar seekBar) { isStartTrackingTouch = true; } //結束觸發 public void onStopTrackingTouch(SeekBar seekBar) { player.seekTo(seekBar.getProgress()); isStartTrackingTouch = false; } } private void generateListView() { List list = new ArrayList(); //獲取sdcard中的所有歌曲 findAll(Environment.getExternalStorageDirectory(), list); //播放列表進行排序,字符順序 Collections.sort(list); data = new ArrayList>(); for (File file : list) { Map map = new HashMap(); map.put("name", file.getName()); map.put("path", file.getAbsolutePath()); data.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[] { "name" }, new int[] { R.id.mName }); listView.setAdapter(adapter); listView.setOnItemClickListener(new MyItemListener()); } private final class MyItemListener implements OnItemClickListener { public void onItemClick(AdapterViewparent, View view, int position, long id) { current = position; play(); } } private void play() { try { //重播 player.reset(); //獲取歌曲路徑 player.setDataSource(data.get(current).get("path")); //緩沖 player.prepare(); //開始播放 player.start(); //顯示歌名 nameTextView.setText(data.get(current).get("name")); //設置進度條長度 seekBar.setMax(player.getDuration()); //播放按鈕樣式 ppButton.setText("||"); //發送一個Runnable, handler收到之後就會執行run()方法 handler.post(new Runnable() { public void run() { // 更新進度條狀態 if (!isStartTrackingTouch) seekBar.setProgress(player.getCurrentPosition()); // 1秒之後再次發送 handler.postDelayed(this, 1000); } }); } catch (Exception e) { e.printStackTrace(); } } private void findAll(File file, List list) { File[] subFiles = file.listFiles(); if (subFiles != null) for (File subFile : subFiles) { if (subFile.isFile() && subFile.getName().endsWith(".mp3")) list.add(subFile); else if (subFile.isDirectory())//如果是目錄 findAll(subFile, list); //遞歸 } } public void pp(View view) { //默認從第一首歌曲開始播放 if (!player.isPlaying() && !isPause) { play(); return; } Button button = (Button) view; //暫停/播放按鈕 if ("||".equals(button.getText())) { pause(); button.setText("?"); } else { resume(); button.setText("||"); } } private void resume() { if (isPause) { player.start(); isPause = false; } } private void pause() { if (player != null && player.isPlaying()) { player.pause(); isPause = true; } } private final class PhoneListener extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { pause(); } } protected void onResume() { super.onResume(); resume(); } }