Android音乐播放器开发

时间:2023-03-09 14:48:03
Android音乐播放器开发

今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是。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

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

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

Android音乐播放器开发Android音乐播放器开发

代码说明一切:

一、MainActivity。java类中的代码:

  1. package com.cn.daming;
  2. import java.io.File;
  3. import java.io.FilenameFilter;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import android.app.ListActivity;
  8. import android.graphics.Color;
  9. import android.graphics.drawable.GradientDrawable;
  10. import android.graphics.drawable.GradientDrawable.Orientation;
  11. import android.media.MediaPlayer;
  12. import android.media.MediaPlayer.OnCompletionListener;
  13. import android.os.Bundle;
  14. import android.os.Handler;
  15. import android.view.KeyEvent;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.ImageButton;
  20. import android.widget.ListView;
  21. import android.widget.SeekBar;
  22. import android.widget.SeekBar.OnSeekBarChangeListener;
  23. import android.widget.TextView;
  24. public class MainActivity extends ListActivity {
  25. private ImageButton mFrontImageButton = null;
  26. private ImageButton mStopImageButton = null;
  27. private ImageButton mStartImageButton = null;
  28. private ImageButton mPauseImageButton = null;
  29. private ImageButton mNextImageButton = null;
  30. /*定义进度handler,显示百分比进度*/
  31. Handler mPercentHandler = new Handler();
  32. private SeekBar     mSeekBar=null;
  33. private TextView curProgressText=null;
  34. private TextView curtimeAndTotaltime=null;
  35. public MediaPlayer mMediaPlayer;
  36. private List<String> mMusicList = new ArrayList<String>();
  37. private int currentListItem = 0;
  38. private static final String MUSIC_PATH = new String("/sdcard/");
  39. @Override
  40. public void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. drawBackground();
  43. setContentView(R.layout.main);
  44. musicList();
  45. mMediaPlayer = new MediaPlayer();
  46. initmFrontMusic();
  47. initStopMusic();
  48. initStartMusic();
  49. initPauseMusic();
  50. initNextMusic();
  51. initSeekBar();
  52. }
  53. public void drawBackground()
  54. {
  55. GradientDrawable grad = new GradientDrawable(
  56. Orientation.TL_BR,
  57. new int[] {
  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. public void initmFrontMusic()
  69. {
  70. mFrontImageButton = (ImageButton)findViewById(R.id.front_button);
  71. mFrontImageButton.setOnClickListener(new OnClickListener(){
  72. public void onClick(View arg0) {
  73. if(--currentListItem >= 0){
  74. currentListItem = mMusicList.size();
  75. }else{
  76. playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
  77. }
  78. }
  79. });
  80. }
  81. public void initStopMusic()
  82. {
  83. mStopImageButton = (ImageButton)findViewById(R.id.stop_button);
  84. mStopImageButton.setOnClickListener(new OnClickListener(){
  85. public void onClick(View arg0) {
  86. if(mMediaPlayer.isPlaying())
  87. {
  88. mMediaPlayer.reset();
  89. }
  90. }
  91. });
  92. }
  93. public void initStartMusic()
  94. {
  95. mStartImageButton = (ImageButton)findViewById(R.id.start_button);
  96. mStartImageButton.setOnClickListener(new OnClickListener(){
  97. public void onClick(View arg0) {
  98. playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
  99. startSeekBarUpdate();
  100. }
  101. });
  102. }
  103. public void initPauseMusic()
  104. {
  105. mPauseImageButton = (ImageButton)findViewById(R.id.pause_button);
  106. mPauseImageButton.setOnClickListener(new OnClickListener(){
  107. public void onClick(View arg0) {
  108. if(mMediaPlayer.isPlaying()){
  109. mMediaPlayer.pause();
  110. }
  111. else{
  112. mMediaPlayer.start();
  113. }
  114. }
  115. });
  116. }
  117. public void initNextMusic()
  118. {
  119. mNextImageButton = (ImageButton)findViewById(R.id.next_button);
  120. mNextImageButton.setOnClickListener(new OnClickListener(){
  121. public void onClick(View arg0) {
  122. nextMusic();
  123. }
  124. });
  125. }
  126. public void initSeekBar()
  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(new OnSeekBarChangeListener() {
  133. public void onProgressChanged(SeekBar seekBar, int progress,
  134. boolean fromUser) {
  135. /* 如果拖动进度发生改变,则显示当前进度值 */
  136. curProgressText.setText("当前进度: " + progress);
  137. }
  138. public void onStartTrackingTouch(SeekBar arg0) {
  139. curProgressText.setText("拖动中...");
  140. }
  141. public void onStopTrackingTouch(SeekBar arg0) {
  142. int dest = mSeekBar.getProgress();
  143. int mMax = mMediaPlayer.getDuration();
  144. int sMax = mSeekBar.getMax();
  145. mMediaPlayer.seekTo(mMax*dest/sMax);
  146. }
  147. });
  148. }
  149. private void playMusic(String path)
  150. {
  151. try {
  152. mMediaPlayer.reset();
  153. mMediaPlayer.setDataSource(path);
  154. mMediaPlayer.prepare();
  155. mMediaPlayer.start();
  156. mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
  157. public void onCompletion(MediaPlayer arg0) {
  158. nextMusic();
  159. }
  160. });
  161. }catch (IOException e) {
  162. e.printStackTrace();
  163. }
  164. }
  165. private void nextMusic()
  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. public boolean onKeyDown(int keyCode, KeyEvent event) {
  178. if(keyCode == KeyEvent.KEYCODE_BACK){
  179. mMediaPlayer.stop();
  180. mMediaPlayer.release();
  181. }
  182. return super.onKeyDown(keyCode, event);
  183. }
  184. @Override
  185. protected void onListItemClick(ListView l, View v, int position, long id) {
  186. currentListItem = position;
  187. playMusic(MUSIC_PATH + mMusicList.get(position));
  188. super.onListItemClick(l, v, position, id);
  189. }
  190. //播放列表
  191. public void musicList()
  192. {
  193. File home = new File(MUSIC_PATH);
  194. if(home.listFiles(new MusicFilter()).length > 0)
  195. {
  196. for(File file : home.listFiles(new MusicFilter()))
  197. {
  198. mMusicList.add(file.getName());
  199. }
  200. ArrayAdapter<String> musicList = new ArrayAdapter<String>(MainActivity.this,R.layout.musicitem,mMusicList);
  201. setListAdapter(musicList);
  202. }
  203. }
  204. /*更新拖动条进度*/
  205. public void startSeekBarUpdate() {
  206. mPercentHandler.post(start);
  207. }
  208. Runnable start = new Runnable() {
  209. public void run() {
  210. // 用一个handler更新SeekBar
  211. mPercentHandler.post(updatesb);
  212. }
  213. };
  214. Runnable updatesb =new Runnable(){
  215. public void run() {
  216. int position = mMediaPlayer.getCurrentPosition();
  217. int mMax = mMediaPlayer.getDuration();
  218. int sMax = 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. class MusicFilter implements FilenameFilter
  228. {
  229. public boolean accept(File dir, String name) {
  230. //这里还可以设置其他格式的音乐文件
  231. return (name.endsWith(".mp3"));
  232. }
  233. }
  234. }

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

  1. <span style="font-size: 13px; color: #000000;"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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布局文件的代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns: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文件

    1. <span style="font-size: 13px; color: #000000;"><?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3. package="com.cn.daming"
    4. android:versionCode="1"
    5. android:versionName="1.0">
    6. <uses-sdk android:minSdkVersion="8" />
    7. <application android:icon="@drawable/icon" android:label="@string/app_name">
    8. <activity android:name=".MainActivity"
    9. android:label="@string/app_name">
    10. <intent-filter>
    11. <action android:name="android.intent.action.MAIN" />
    12. <category android:name="android.intent.category.LAUNCHER" />
    13. </intent-filter>
    14. </activity>
    15. </application>
    16. </manifest></span>