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

 

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

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

                                 

 

代码说明一切:

一、MainActivity。java类中的代码:

[java]  view plain copy print ?
  1. package com.cn.daming;  
  2.   
  3. import java.io.File;  
  4. import java.io.FilenameFilter;  
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import android.app.ListActivity;  
  10. import android.graphics.Color;  
  11. import android.graphics.drawable.GradientDrawable;  
  12. import android.graphics.drawable.GradientDrawable.Orientation;  
  13. import android.media.MediaPlayer;  
  14. import android.media.MediaPlayer.OnCompletionListener;  
  15. import android.os.Bundle;  
  16. import android.os.Handler;  
  17. import android.view.KeyEvent;  
  18. import android.view.View;  
  19. import android.view.View.OnClickListener;  
  20. import android.widget.ArrayAdapter;  
  21. import android.widget.ImageButton;  
  22. import android.widget.ListView;  
  23. import android.widget.SeekBar;  
  24. import android.widget.SeekBar.OnSeekBarChangeListener;  
  25. import android.widget.TextView;  
  26.   
  27. public class MainActivity extends ListActivity {  
  28.   
  29.     private ImageButton mFrontImageButton = null;  
  30.     private ImageButton mStopImageButton = null;  
  31.     private ImageButton mStartImageButton = null;  
  32.     private ImageButton mPauseImageButton = null;  
  33.     private ImageButton mNextImageButton = null;  
  34.   
  35.     /*定义进度handler,显示百分比进度*/  
  36.     Handler mPercentHandler = new Handler();  
  37.   
  38.     private SeekBar     mSeekBar=null;  
  39.     private TextView curProgressText=null;  
  40.     private TextView curtimeAndTotaltime=null;  
  41.   
  42.       
  43.     public MediaPlayer mMediaPlayer;  
  44.     private List<String> mMusicList = new ArrayList<String>();  
  45.     private int currentListItem = 0;  
  46.       
  47.     private static final String MUSIC_PATH = new String("/sdcard/");  
  48.       
  49.     @Override  
  50.     public void onCreate(Bundle savedInstanceState) {  
  51.         super.onCreate(savedInstanceState);  
  52.           
  53.         drawBackground();    
  54.         setContentView(R.layout.main);  
  55.         musicList();  
  56.         mMediaPlayer = new MediaPlayer();  
  57.         initmFrontMusic();  
  58.         initStopMusic();  
  59.         initStartMusic();  
  60.         initPauseMusic();  
  61.         initNextMusic();  
  62.         initSeekBar();  
  63.     }  
  64.       
  65.      public void drawBackground()    
  66.          {    
  67.              GradientDrawable grad = new GradientDrawable(     
  68.                         Orientation.TL_BR,    
  69.                         new int[] {  
  70.                                        Color.rgb(00127),    
  71.                                        Color.rgb(00255),    
  72.                                        Color.rgb(1270255),    
  73.                                        Color.rgb(127127255),    
  74.                                        Color.rgb(127255255),    
  75.                                        Color.rgb(255255255)  
  76.                                    }     
  77.              );     
  78.          
  79.              this.getWindow().setBackgroundDrawable(grad);    
  80.          }    
  81.   
  82.       
  83.     public void initmFrontMusic()  
  84.     {  
  85.         mFrontImageButton = (ImageButton)findViewById(R.id.front_button);  
  86.         mFrontImageButton.setOnClickListener(new OnClickListener(){  
  87.   
  88.             public void onClick(View arg0) {  
  89.                 if(--currentListItem >= 0){  
  90.                     currentListItem = mMusicList.size();  
  91.                 }else{  
  92.                     playMusic(MUSIC_PATH + mMusicList.get(currentListItem));  
  93.                 }  
  94.             }  
  95.         });  
  96.     }  
  97.       
  98.     public void initStopMusic()  
  99.     {  
  100.         mStopImageButton = (ImageButton)findViewById(R.id.stop_button);  
  101.         mStopImageButton.setOnClickListener(new OnClickListener(){  
  102.   
  103.             public void onClick(View arg0) {  
  104.                 if(mMediaPlayer.isPlaying())  
  105.                 {  
  106.                     mMediaPlayer.reset();  
  107.                 }  
  108.             }  
  109.         });  
  110.     }  
  111.       
  112.     public void initStartMusic()  
  113.     {  
  114.         mStartImageButton = (ImageButton)findViewById(R.id.start_button);  
  115.         mStartImageButton.setOnClickListener(new OnClickListener(){  
  116.   
  117.             public void onClick(View arg0) {  
  118.                 playMusic(MUSIC_PATH + mMusicList.get(currentListItem));  
  119.                 startSeekBarUpdate();  
  120.             }  
  121.         });  
  122.     }  
  123.       
  124.     public void initPauseMusic()  
  125.     {  
  126.         mPauseImageButton = (ImageButton)findViewById(R.id.pause_button);  
  127.         mPauseImageButton.setOnClickListener(new OnClickListener(){  
  128.   
  129.             public void onClick(View arg0) {  
  130.                 if(mMediaPlayer.isPlaying()){  
  131.                     mMediaPlayer.pause();  
  132.                 }  
  133.                 else{  
  134.                     mMediaPlayer.start();  
  135.                 }  
  136.             }  
  137.         });  
  138.     }  
  139.       
  140.     public void initNextMusic()  
  141.     {  
  142.         mNextImageButton = (ImageButton)findViewById(R.id.next_button);  
  143.         mNextImageButton.setOnClickListener(new OnClickListener(){  
  144.   
  145.             public void onClick(View arg0) {  
  146.                 nextMusic();  
  147.             }  
  148.         });  
  149.     }  
  150.       
  151.     public void initSeekBar()  
  152.     {  
  153.         /*初始化拖动条和当前进度显示值*/  
  154.         mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);  
  155.         curProgressText=(TextView)findViewById(R.id.currentProgress);  
  156.         curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);  
  157.           
  158.         mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  159.   
  160.             public void onProgressChanged(SeekBar seekBar, int progress,  
  161.                     boolean fromUser) {  
  162.                 /* 如果拖动进度发生改变,则显示当前进度值 */  
  163.                 curProgressText.setText("当前进度: " + progress);  
  164.             }  
  165.   
  166.             public void onStartTrackingTouch(SeekBar arg0) {  
  167.                 curProgressText.setText("拖动中...");  
  168.             }  
  169.   
  170.             public void onStopTrackingTouch(SeekBar arg0) {  
  171.                 int dest = mSeekBar.getProgress();      
  172.                 int mMax = mMediaPlayer.getDuration();  
  173.                 int sMax = mSeekBar.getMax();  
  174.   
  175.                 mMediaPlayer.seekTo(mMax*dest/sMax);  
  176.   
  177.             }  
  178.         });  
  179.     }  
  180.       
  181.     private void playMusic(String path)  
  182.     {  
  183.         try {  
  184.             mMediaPlayer.reset();  
  185.             mMediaPlayer.setDataSource(path);  
  186.             mMediaPlayer.prepare();  
  187.             mMediaPlayer.start();  
  188.             mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){  
  189.   
  190.                 public void onCompletion(MediaPlayer arg0) {  
  191.                     nextMusic();  
  192.                 }  
  193.             });  
  194.          }catch (IOException e) {  
  195.             e.printStackTrace();  
  196.         }  
  197.     }  
  198.       
  199.     private void nextMusic()  
  200.     {  
  201.         if(++currentListItem >= mMusicList.size())  
  202.         {  
  203.             currentListItem = 0;  
  204.         }  
  205.         else  
  206.         {  
  207.             playMusic(MUSIC_PATH + mMusicList.get(currentListItem));  
  208.         }  
  209.     }  
  210.       
  211.     @Override  
  212.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  213.         if(keyCode == KeyEvent.KEYCODE_BACK){  
  214.             mMediaPlayer.stop();  
  215.             mMediaPlayer.release();  
  216.         }  
  217.         return super.onKeyDown(keyCode, event);  
  218.     }  
  219.   
  220.     @Override  
  221.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  222.         currentListItem = position;  
  223.         playMusic(MUSIC_PATH + mMusicList.get(position));  
  224.         super.onListItemClick(l, v, position, id);  
  225.     }  
  226.   
  227.     //播放列表  
  228.     public void musicList()  
  229.     {  
  230.         File home = new File(MUSIC_PATH);  
  231.         if(home.listFiles(new MusicFilter()).length > 0)  
  232.         {  
  233.             for(File file : home.listFiles(new MusicFilter()))  
  234.             {  
  235.                 mMusicList.add(file.getName());  
  236.             }  
  237.             ArrayAdapter<String> musicList = new ArrayAdapter<String>(MainActivity.this,R.layout.musicitem,mMusicList);  
  238.             setListAdapter(musicList);  
  239.         }  
  240.     }  
  241.       
  242.     /*更新拖动条进度*/  
  243.   
  244.     public void startSeekBarUpdate() {  
  245.         mPercentHandler.post(start);  
  246.     }  
  247.   
  248.     Runnable start = new Runnable() {  
  249.   
  250.         public void run() {  
  251.             // 用一个handler更新SeekBar  
  252.             mPercentHandler.post(updatesb);  
  253.         }  
  254.   
  255.     };  
  256.   
  257.     Runnable updatesb =new Runnable(){  
  258.   
  259.     public void run() {  
  260.             int position = mMediaPlayer.getCurrentPosition();  
  261.             int mMax = mMediaPlayer.getDuration();  
  262.             int sMax = mSeekBar.getMax();  
  263.             mSeekBar.setProgress(position * sMax / mMax);  
  264.             curtimeAndTotaltime.setText("当前播放时间: " + position / 1000 + "秒"  
  265.                     + "\n歌曲总时间: " + mMax / 1000 + "秒");  
  266.             // 每秒钟更新一次  
  267.             mPercentHandler.postDelayed(updatesb, 1000);  
  268.     }  
  269.   
  270.     };  
  271.       
  272.   
  273.     //过滤文件类型  
  274.     class MusicFilter implements FilenameFilter  
  275.     {  
  276.   
  277.         public boolean accept(File dir, String name) {  
  278.             //这里还可以设置其他格式的音乐文件  
  279.             return (name.endsWith(".mp3"));  
  280.         }  
  281.     }  
  282. }  

 

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

[html]  view plain copy print ?
  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.        
  24.      <SeekBar   
  25.         android:id="@+id/SeekBar01"   
  26.         android:layout_height="wrap_content"   
  27.         android:layout_width="fill_parent"   
  28.         android:max="100"   
  29.         android:progress="0"   
  30.         android:secondaryProgress="0"   
  31.         android:visibility="visible"  
  32.        />  
  33.   
  34.     <TextView   
  35.         android:layout_height="wrap_content"  
  36.         android:layout_width="fill_parent"   
  37.         android:id="@+id/currentProgress"  
  38.     />  
  39.   
  40.    <TextView   
  41.         android:layout_height="wrap_content"   
  42.         android:layout_width="fill_parent"   
  43.         android:layout_y="300dp"   
  44.         android:id="@+id/curtimeandtotaltime"  
  45.     />  
  46.        
  47.        
  48.      <LinearLayout   
  49.         android:orientation="horizontal"  
  50.         android:layout_width="fill_parent"  
  51.         android:layout_height="wrap_content"  
  52.       >  
  53.           <ImageButton  
  54.                android:id="@+id/front_button"  
  55.                android:layout_width="wrap_content"  
  56.                android:layout_height="wrap_content"  
  57.                android:background="@drawable/first1"  
  58.                android:layout_marginLeft="10dip"  
  59.           />  
  60.           <ImageButton  
  61.                android:id="@+id/stop_button"  
  62.                android:layout_width="wrap_content"  
  63.                android:layout_height="wrap_content"  
  64.                android:background="@drawable/stop1"  
  65.                android:layout_marginLeft="10dip"  
  66.           />  
  67.           <ImageButton  
  68.                android:id="@+id/start_button"  
  69.                android:layout_width="wrap_content"  
  70.                android:layout_height="wrap_content"  
  71.                android:background="@drawable/start1"  
  72.                android:layout_marginLeft="10dip"  
  73.           />  
  74.           <ImageButton  
  75.                android:id="@+id/pause_button"  
  76.                android:layout_width="wrap_content"  
  77.                android:layout_height="wrap_content"  
  78.                android:background="@drawable/pose1"  
  79.                android:layout_marginLeft="10dip"  
  80.           />  
  81.           <ImageButton  
  82.                android:id="@+id/next_button"  
  83.                android:layout_width="wrap_content"  
  84.                android:layout_height="wrap_content"  
  85.                android:background="@drawable/next1"  
  86.                android:layout_marginLeft="10dip"  
  87.           />  
  88.       </LinearLayout>  
  89. </LinearLayout>  
  90. </span>  

 

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

[html]  view plain copy print ?
  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文件

[html]  view plain copy print ?
  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.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18. </manifest></span>  


 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值