【郭林专刊】Android Music

音乐播放器中综合了以下内容:

SeekBar、ListView、广播接收者(以代码的形式注册Receiver)、系统服务、MediaPlayer

实现的功能:

1.暂停/播放、下一首/上一首,点击某一首时播放

2.支持拖动进度条快进

3.列表排序

4.来电话时,停止播放,挂断后继续播放

5.可在后台播放

效果图:

界面:

main.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/name"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. <SeekBar
  13. android:id="@+id/seekBar"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_marginBottom="5dp"
  17. />
  18. <LinearLayout
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginBottom="20dp"
  22. >
  23. <Button
  24. android:layout_width="40dp"
  25. android:layout_height="40dp"
  26. android:text="|◀"
  27. android:onClick="previous"
  28. />
  29. <Button
  30. android:id="@+id/pp"
  31. android:layout_width="40dp"
  32. android:layout_height="40dp"
  33. android:text="▶"
  34. android:onClick="pp"
  35. />
  36. <Button
  37. android:layout_width="40dp"
  38. android:layout_height="40dp"
  39. android:text="▶|"
  40. android:onClick="next"
  41. />
  42. </LinearLayout>
  43. <ListView
  44. android:id="@+id/list"
  45. android:layout_width="fill_parent"
  46. android:layout_height="fill_parent"
  47. />
  48. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" > <Button android:layout_width="40dp" android:layout_height="40dp" android:text="|◀" android:onClick="previous" /> <Button android:id="@+id/pp" android:layout_width="40dp" android:layout_height="40dp" android:text="▶" android:onClick="pp" /> <Button android:layout_width="40dp" android:layout_height="40dp" android:text="▶|" android:onClick="next" /> </LinearLayout> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>


item.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:padding="10dp"
  7. >
  8. <TextView
  9. android:id="@+id/mName"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:textSize="15sp"
  13. />
  14. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" > <TextView android:id="@+id/mName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15sp" /> </LinearLayout>

MainActivity:

  1. packagecn.test.audio;
  2. importjava.io.File;
  3. importjava.util.ArrayList;
  4. importjava.util.Collections;
  5. importjava.util.HashMap;
  6. importjava.util.List;
  7. importjava.util.Map;
  8. importandroid.app.Activity;
  9. importandroid.content.BroadcastReceiver;
  10. importandroid.content.Context;
  11. importandroid.content.Intent;
  12. importandroid.content.IntentFilter;
  13. importandroid.media.MediaPlayer;
  14. importandroid.media.MediaPlayer.OnCompletionListener;
  15. importandroid.os.Bundle;
  16. importandroid.os.Environment;
  17. importandroid.os.Handler;
  18. importandroid.telephony.PhoneStateListener;
  19. importandroid.telephony.TelephonyManager;
  20. importandroid.view.View;
  21. importandroid.widget.AdapterView;
  22. importandroid.widget.Button;
  23. importandroid.widget.ListView;
  24. importandroid.widget.SeekBar;
  25. importandroid.widget.SimpleAdapter;
  26. importandroid.widget.TextView;
  27. importandroid.widget.AdapterView.OnItemClickListener;
  28. importandroid.widget.SeekBar.OnSeekBarChangeListener;
  29. publicclassMainActivityextendsActivity{
  30. privateTextViewnameTextView;
  31. privateSeekBarseekBar;
  32. privateListViewlistView;
  33. privateList<Map<String,String>>data;
  34. privateintcurrent;
  35. privateMediaPlayerplayer;
  36. privateHandlerhandler=newHandler();
  37. privateButtonppButton;
  38. privatebooleanisPause;
  39. privatebooleanisStartTrackingTouch;
  40. publicvoidonCreate(BundlesavedInstanceState){
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.main);
  43. nameTextView=(TextView)findViewById(R.id.name);
  44. seekBar=(SeekBar)findViewById(R.id.seekBar);
  45. listView=(ListView)findViewById(R.id.list);
  46. ppButton=(Button)findViewById(R.id.pp);
  47. //创建一个音乐播放器
  48. player=newMediaPlayer();
  49. //显示音乐播放列表
  50. generateListView();
  51. //进度条监听器
  52. seekBar.setOnSeekBarChangeListener(newMySeekBarListener());
  53. //播放器监听器
  54. player.setOnCompletionListener(newMyPlayerListener());
  55. //意图过滤器
  56. IntentFilterfilter=newIntentFilter();
  57. //播出电话暂停音乐播放
  58. filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
  59. registerReceiver(newPhoneListener(),filter);
  60. //创建一个电话服务
  61. TelephonyManagermanager=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
  62. //监听电话状态,接电话时停止播放
  63. manager.listen(newMyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);
  64. }
  65. /*
  66. *监听电话状态
  67. */
  68. privatefinalclassMyPhoneStateListenerextendsPhoneStateListener{
  69. publicvoidonCallStateChanged(intstate,StringincomingNumber){
  70. pause();
  71. }
  72. }
  73. /*
  74. *播放器监听器
  75. */
  76. privatefinalclassMyPlayerListenerimplementsOnCompletionListener{
  77. //歌曲播放完后自动播放下一首歌曲
  78. publicvoidonCompletion(MediaPlayermp){
  79. next();
  80. }
  81. }
  82. /*
  83. *下一首按钮
  84. */
  85. publicvoidnext(Viewview){
  86. next();
  87. }
  88. /*
  89. *前一首按钮
  90. */
  91. publicvoidprevious(Viewview){
  92. previous();
  93. }
  94. /*
  95. *播放前一首歌
  96. */
  97. privatevoidprevious(){
  98. current=current-1<0?data.size()-1:current-1;
  99. play();
  100. }
  101. /*
  102. *播放下一首歌
  103. */
  104. privatevoidnext(){
  105. current=(current+1)%data.size();
  106. play();
  107. }
  108. /*
  109. *进度条监听器
  110. */
  111. privatefinalclassMySeekBarListenerimplementsOnSeekBarChangeListener{
  112. //移动触发
  113. publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){
  114. }
  115. //起始触发
  116. publicvoidonStartTrackingTouch(SeekBarseekBar){
  117. isStartTrackingTouch=true;
  118. }
  119. //结束触发
  120. publicvoidonStopTrackingTouch(SeekBarseekBar){
  121. player.seekTo(seekBar.getProgress());
  122. isStartTrackingTouch=false;
  123. }
  124. }
  125. /*
  126. *显示音乐播放列表
  127. */
  128. privatevoidgenerateListView(){
  129. List<File>list=newArrayList<File>();
  130. //获取sdcard中的所有歌曲
  131. findAll(Environment.getExternalStorageDirectory(),list);
  132. //播放列表进行排序,字符顺序
  133. Collections.sort(list);
  134. data=newArrayList<Map<String,String>>();
  135. for(Filefile:list){
  136. Map<String,String>map=newHashMap<String,String>();
  137. map.put("name",file.getName());
  138. map.put("path",file.getAbsolutePath());
  139. data.add(map);
  140. }
  141. SimpleAdapteradapter=newSimpleAdapter(this,data,R.layout.item,newString[]{"name"},newint[]{R.id.mName});
  142. listView.setAdapter(adapter);
  143. listView.setOnItemClickListener(newMyItemListener());
  144. }
  145. privatefinalclassMyItemListenerimplementsOnItemClickListener{
  146. publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,longid){
  147. current=position;
  148. play();
  149. }
  150. }
  151. privatevoidplay(){
  152. try{
  153. //重播
  154. player.reset();
  155. //获取歌曲路径
  156. player.setDataSource(data.get(current).get("path"));
  157. //缓冲
  158. player.prepare();
  159. //开始播放
  160. player.start();
  161. //显示歌名
  162. nameTextView.setText(data.get(current).get("name"));
  163. //设置进度条长度
  164. seekBar.setMax(player.getDuration());
  165. //播放按钮样式
  166. ppButton.setText("||");
  167. //发送一个Runnable,handler收到之后就会执行run()方法
  168. handler.post(newRunnable(){
  169. publicvoidrun(){
  170. //更新进度条状态
  171. if(!isStartTrackingTouch)
  172. seekBar.setProgress(player.getCurrentPosition());
  173. //1秒之后再次发送
  174. handler.postDelayed(this,1000);
  175. }
  176. });
  177. }catch(Exceptione){
  178. e.printStackTrace();
  179. }
  180. }
  181. /**
  182. *查找文件路径中所有mp3文件
  183. *@paramfile要找的目录
  184. *@paramlist用来装文件的List
  185. */
  186. privatevoidfindAll(Filefile,List<File>list){
  187. File[]subFiles=file.listFiles();
  188. if(subFiles!=null)
  189. for(FilesubFile:subFiles){
  190. if(subFile.isFile()&&subFile.getName().endsWith(".mp3"))
  191. list.add(subFile);
  192. elseif(subFile.isDirectory())//如果是目录
  193. findAll(subFile,list);//递归
  194. }
  195. }
  196. /*
  197. *暂停/播放按钮
  198. */
  199. publicvoidpp(Viewview){
  200. //默认从第一首歌曲开始播放
  201. if(!player.isPlaying()&&!isPause){
  202. play();
  203. return;
  204. }
  205. Buttonbutton=(Button)view;
  206. //暂停/播放按钮
  207. if("||".equals(button.getText())){
  208. pause();
  209. button.setText("▶");
  210. }else{
  211. resume();
  212. button.setText("||");
  213. }
  214. }
  215. /*
  216. *开始操作
  217. */
  218. privatevoidresume(){
  219. if(isPause){
  220. player.start();
  221. isPause=false;
  222. }
  223. }
  224. /*
  225. *暂停操作
  226. */
  227. privatevoidpause(){
  228. if(player!=null&&player.isPlaying()){
  229. player.pause();
  230. isPause=true;
  231. }
  232. }
  233. /*
  234. *收到广播时暂停
  235. */
  236. privatefinalclassPhoneListenerextendsBroadcastReceiver{
  237. publicvoidonReceive(Contextcontext,Intentintent){
  238. pause();
  239. }
  240. }
  241. /*
  242. *恢复播放
  243. *@seeandroid.app.Activity#onResume()
  244. */
  245. protectedvoidonResume(){
  246. super.onResume();
  247. resume();
  248. }
  249. }
package cn.test.audio; 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<Map<String, String>> 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<File> list = new ArrayList<File>(); //获取sdcard中的所有歌曲 findAll(Environment.getExternalStorageDirectory(), list); //播放列表进行排序,字符顺序 Collections.sort(list); data = new ArrayList<Map<String, String>>(); for (File file : list) { Map<String, String> map = new HashMap<String, String>(); 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(AdapterView<?> parent, 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(); } } /** * 查找文件路径中所有mp3文件 * @param file 要找的目录 * @param list 用来装文件的List */ private void findAll(File file, List<File> 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(); } } /* * 恢复播放 * @see android.app.Activity#onResume() */ protected void onResume() { super.onResume(); resume(); } }


注册权限:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.itcast.audio"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".MainActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. <uses-sdkandroid:minSdkVersion="8"/>
  16. <!--监听电话呼出-->
  17. <uses-permissionandroid:name="android.permission.PROCESS_OUTGOING_CALLS"/>
  18. <!--监听电话状态改变-->
  19. <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
  20. </manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.audio" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <!-- 监听电话呼出 --> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <!-- 监听电话状态改变 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> </manifest>

非常反感源码 但是还是共享一下 帮助大家共同学习。。。。。