今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是。MP3的音乐,这个例子在main.xml设置listView的时候,注意:android:id="@+id/android:list"的设置,否则程序会报错,说找不到listview。这个效果还是不错的。可以当做是简单的音乐播放器,可以读取sdcard里面后缀是。MP3的歌曲。有问题可以留言,想要源码可以留言,这个代码比较简单。转载请标明出处:

http://blog.csdn.net/wdaming1986/article/details/6768884

csdn资源下载链接地址http://download.csdn.net/detail/wdaming1986/3611735

看程序效果图:可以点击每首歌播放,

也可以用下面的按钮: 修改后的程序加了滚动条了

代码说明一切:

一、MainActivity。java类中的代码:

[java] view plain copy print ?
  1. packagecom.cn.daming;
  2. importjava.io.File;
  3. importjava.io.FilenameFilter;
  4. importjava.io.IOException;
  5. importjava.util.ArrayList;
  6. importjava.util.List;
  7. importandroid.app.ListActivity;
  8. importandroid.graphics.Color;
  9. importandroid.graphics.drawable.GradientDrawable;
  10. importandroid.graphics.drawable.GradientDrawable.Orientation;
  11. importandroid.media.MediaPlayer;
  12. importandroid.media.MediaPlayer.OnCompletionListener;
  13. importandroid.os.Bundle;
  14. importandroid.os.Handler;
  15. importandroid.view.KeyEvent;
  16. importandroid.view.View;
  17. importandroid.view.View.OnClickListener;
  18. importandroid.widget.ArrayAdapter;
  19. importandroid.widget.ImageButton;
  20. importandroid.widget.ListView;
  21. importandroid.widget.SeekBar;
  22. importandroid.widget.SeekBar.OnSeekBarChangeListener;
  23. importandroid.widget.TextView;
  24. publicclassMainActivityextendsListActivity{
  25. privateImageButtonmFrontImageButton=null;
  26. privateImageButtonmStopImageButton=null;
  27. privateImageButtonmStartImageButton=null;
  28. privateImageButtonmPauseImageButton=null;
  29. privateImageButtonmNextImageButton=null;
  30. /*定义进度handler,显示百分比进度*/
  31. HandlermPercentHandler=newHandler();
  32. privateSeekBarmSeekBar=null;
  33. privateTextViewcurProgressText=null;
  34. privateTextViewcurtimeAndTotaltime=null;
  35. publicMediaPlayermMediaPlayer;
  36. privateList<String>mMusicList=newArrayList<String>();
  37. privateintcurrentListItem=0;
  38. privatestaticfinalStringMUSIC_PATH=newString("/sdcard/");
  39. @Override
  40. publicvoidonCreate(BundlesavedInstanceState){
  41. super.onCreate(savedInstanceState);
  42. drawBackground();
  43. setContentView(R.layout.main);
  44. musicList();
  45. mMediaPlayer=newMediaPlayer();
  46. initmFrontMusic();
  47. initStopMusic();
  48. initStartMusic();
  49. initPauseMusic();
  50. initNextMusic();
  51. initSeekBar();
  52. }
  53. publicvoiddrawBackground()
  54. {
  55. GradientDrawablegrad=newGradientDrawable(
  56. Orientation.TL_BR,
  57. newint[]{
  58. Color.rgb(0,0,127),
  59. Color.rgb(0,0,255),
  60. Color.rgb(127,0,255),
  61. Color.rgb(127,127,255),
  62. Color.rgb(127,255,255),
  63. Color.rgb(255,255,255)
  64. }
  65. );
  66. this.getWindow().setBackgroundDrawable(grad);
  67. }
  68. publicvoidinitmFrontMusic()
  69. {
  70. mFrontImageButton=(ImageButton)findViewById(R.id.front_button);
  71. mFrontImageButton.setOnClickListener(newOnClickListener(){
  72. publicvoidonClick(Viewarg0){
  73. if(--currentListItem>=0){
  74. currentListItem=mMusicList.size();
  75. }else{
  76. playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
  77. }
  78. }
  79. });
  80. }
  81. publicvoidinitStopMusic()
  82. {
  83. mStopImageButton=(ImageButton)findViewById(R.id.stop_button);
  84. mStopImageButton.setOnClickListener(newOnClickListener(){
  85. publicvoidonClick(Viewarg0){
  86. if(mMediaPlayer.isPlaying())
  87. {
  88. mMediaPlayer.reset();
  89. }
  90. }
  91. });
  92. }
  93. publicvoidinitStartMusic()
  94. {
  95. mStartImageButton=(ImageButton)findViewById(R.id.start_button);
  96. mStartImageButton.setOnClickListener(newOnClickListener(){
  97. publicvoidonClick(Viewarg0){
  98. playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
  99. startSeekBarUpdate();
  100. }
  101. });
  102. }
  103. publicvoidinitPauseMusic()
  104. {
  105. mPauseImageButton=(ImageButton)findViewById(R.id.pause_button);
  106. mPauseImageButton.setOnClickListener(newOnClickListener(){
  107. publicvoidonClick(Viewarg0){
  108. if(mMediaPlayer.isPlaying()){
  109. mMediaPlayer.pause();
  110. }
  111. else{
  112. mMediaPlayer.start();
  113. }
  114. }
  115. });
  116. }
  117. publicvoidinitNextMusic()
  118. {
  119. mNextImageButton=(ImageButton)findViewById(R.id.next_button);
  120. mNextImageButton.setOnClickListener(newOnClickListener(){
  121. publicvoidonClick(Viewarg0){
  122. nextMusic();
  123. }
  124. });
  125. }
  126. publicvoidinitSeekBar()
  127. {
  128. /*初始化拖动条和当前进度显示值*/
  129. mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);
  130. curProgressText=(TextView)findViewById(R.id.currentProgress);
  131. curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);
  132. mSeekBar.setOnSeekBarChangeListener(newOnSeekBarChangeListener(){
  133. publicvoidonProgressChanged(SeekBarseekBar,intprogress,
  134. booleanfromUser){
  135. /*如果拖动进度发生改变,则显示当前进度值*/
  136. curProgressText.setText("当前进度:"+progress);
  137. }
  138. publicvoidonStartTrackingTouch(SeekBararg0){
  139. curProgressText.setText("拖动中...");
  140. }
  141. publicvoidonStopTrackingTouch(SeekBararg0){
  142. intdest=mSeekBar.getProgress();
  143. intmMax=mMediaPlayer.getDuration();
  144. intsMax=mSeekBar.getMax();
  145. mMediaPlayer.seekTo(mMax*dest/sMax);
  146. }
  147. });
  148. }
  149. privatevoidplayMusic(Stringpath)
  150. {
  151. try{
  152. mMediaPlayer.reset();
  153. mMediaPlayer.setDataSource(path);
  154. mMediaPlayer.prepare();
  155. mMediaPlayer.start();
  156. mMediaPlayer.setOnCompletionListener(newOnCompletionListener(){
  157. publicvoidonCompletion(MediaPlayerarg0){
  158. nextMusic();
  159. }
  160. });
  161. }catch(IOExceptione){
  162. e.printStackTrace();
  163. }
  164. }
  165. privatevoidnextMusic()
  166. {
  167. if(++currentListItem>=mMusicList.size())
  168. {
  169. currentListItem=0;
  170. }
  171. else
  172. {
  173. playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
  174. }
  175. }
  176. @Override
  177. publicbooleanonKeyDown(intkeyCode,KeyEventevent){
  178. if(keyCode==KeyEvent.KEYCODE_BACK){
  179. mMediaPlayer.stop();
  180. mMediaPlayer.release();
  181. }
  182. returnsuper.onKeyDown(keyCode,event);
  183. }
  184. @Override
  185. protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
  186. currentListItem=position;
  187. playMusic(MUSIC_PATH+mMusicList.get(position));
  188. super.onListItemClick(l,v,position,id);
  189. }
  190. //播放列表
  191. publicvoidmusicList()
  192. {
  193. Filehome=newFile(MUSIC_PATH);
  194. if(home.listFiles(newMusicFilter()).length>0)
  195. {
  196. for(Filefile:home.listFiles(newMusicFilter()))
  197. {
  198. mMusicList.add(file.getName());
  199. }
  200. ArrayAdapter<String>musicList=newArrayAdapter<String>(MainActivity.this,R.layout.musicitem,mMusicList);
  201. setListAdapter(musicList);
  202. }
  203. }
  204. /*更新拖动条进度*/
  205. publicvoidstartSeekBarUpdate(){
  206. mPercentHandler.post(start);
  207. }
  208. Runnablestart=newRunnable(){
  209. publicvoidrun(){
  210. //用一个handler更新SeekBar
  211. mPercentHandler.post(updatesb);
  212. }
  213. };
  214. Runnableupdatesb=newRunnable(){
  215. publicvoidrun(){
  216. intposition=mMediaPlayer.getCurrentPosition();
  217. intmMax=mMediaPlayer.getDuration();
  218. intsMax=mSeekBar.getMax();
  219. mSeekBar.setProgress(position*sMax/mMax);
  220. curtimeAndTotaltime.setText("当前播放时间:"+position/1000+"秒"
  221. +"\n歌曲总时间:"+mMax/1000+"秒");
  222. //每秒钟更新一次
  223. mPercentHandler.postDelayed(updatesb,1000);
  224. }
  225. };
  226. //过滤文件类型
  227. classMusicFilterimplementsFilenameFilter
  228. {
  229. publicbooleanaccept(Filedir,Stringname){
  230. //这里还可以设置其他格式的音乐文件
  231. return(name.endsWith(".mp3"));
  232. }
  233. }
  234. }

二、main。xml布局文件的代码:

[html] view plain copy print ?
  1. <spanstyle="font-size:13px;color:#000000;"><?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:layout_width="fill_parent"
  9. android:layout_height="25dip"
  10. android:paddingTop="5dip"
  11. android:layout_gravity="center_horizontal"
  12. android:gravity="center_horizontal"
  13. android:textColor="#FF000000"
  14. android:text="大明制作Mp3播放器"
  15. />
  16. <ListView
  17. android:id="@+id/android:list"
  18. android:layout_width="fill_parent"
  19. android:layout_height="200dip"
  20. android:layout_weight="1"
  21. android:drawSelectorOnTop="false"
  22. />
  23. <SeekBar
  24. android:id="@+id/SeekBar01"
  25. android:layout_height="wrap_content"
  26. android:layout_width="fill_parent"
  27. android:max="100"
  28. android:progress="0"
  29. android:secondaryProgress="0"
  30. android:visibility="visible"
  31. />
  32. <TextView
  33. android:layout_height="wrap_content"
  34. android:layout_width="fill_parent"
  35. android:id="@+id/currentProgress"
  36. />
  37. <TextView
  38. android:layout_height="wrap_content"
  39. android:layout_width="fill_parent"
  40. android:layout_y="300dp"
  41. android:id="@+id/curtimeandtotaltime"
  42. />
  43. <LinearLayout
  44. android:orientation="horizontal"
  45. android:layout_width="fill_parent"
  46. android:layout_height="wrap_content"
  47. >
  48. <ImageButton
  49. android:id="@+id/front_button"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:background="@drawable/first1"
  53. android:layout_marginLeft="10dip"
  54. />
  55. <ImageButton
  56. android:id="@+id/stop_button"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:background="@drawable/stop1"
  60. android:layout_marginLeft="10dip"
  61. />
  62. <ImageButton
  63. android:id="@+id/start_button"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:background="@drawable/start1"
  67. android:layout_marginLeft="10dip"
  68. />
  69. <ImageButton
  70. android:id="@+id/pause_button"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:background="@drawable/pose1"
  74. android:layout_marginLeft="10dip"
  75. />
  76. <ImageButton
  77. android:id="@+id/next_button"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"
  80. android:background="@drawable/next1"
  81. android:layout_marginLeft="10dip"
  82. />
  83. </LinearLayout>
  84. </LinearLayout>
  85. </span>

三、musicitem.xml布局文件的代码:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TextViewxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/TextView01"
  4. android:layout_width="fill_parent"
  5. android:layout_height="26dip"
  6. android:layout_gravity="center_vertical"
  7. android:paddingTop="5dip"
  8. android:paddingLeft="20dip"
  9. android:textColor="#FF000000"
  10. android:text="@string/hello1"/>


四、Manifest。xml文件

[html] view plain copy print ?
  1. <spanstyle="font-size:13px;color:#000000;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cn.daming"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="8"/>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. </manifest></span>


更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android(安卓)Studio 使用Git创建本地分支和远程分支以及合并
  3. android EventBus学习记录
  4. Android移动安全(一)Android混淆机制
  5. Android(安卓)Launcher研究(一)-----------图文详解手把手教你在
  6. Android:让WebView支持元素,实现文件上传
  7. Android(安卓)源码下载编译
  8. Android中Context的详细使用
  9. android 手把手教你打造万能的ListView GridView的适配器

随机推荐

  1. android 编译自己的sdk
  2. [译] 在 Android 使用协程(part III) -
  3. android导入多个第三方包
  4. 实战技巧:Android异步指南
  5. Android 个别手机导航键覆盖布局解决办法
  6. Android开发中的MVC
  7. NFC:Arduino、Android与PhoneGap近场通信
  8. Android仿iPhone圆角边框
  9. 从零学Android(八)、Android资源类型之Draw
  10. 深入探究Android的WebView下载网络文件的