zoukankan      html  css  js  c++  java
  • 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代码 
    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(0, 0, 127),    
    71.                                        Color.rgb(0, 0, 255),    
    72.                                        Color.rgb(127, 0, 255),    
    73.                                        Color.rgb(127, 127, 255),    
    74.                                        Color.rgb(127, 255, 255),    
    75.                                        Color.rgb(255, 255, 255)  
    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.                     + " 歌曲总时间: " + 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代码 
    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代码 
    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代码 
      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>  
  • 相关阅读:
    Atitit 图像处理类库 halcon11  安装与环境搭建attilax总结
    Atitit  undac网络设备管理法案 (路由器 交换机等)    法案编号USRr101510
    Atitit 图像处理 halcon类库的使用  范例边缘检测 attilax总结
    Atitit Seed-Filling种子填充算法attilax总结
    Atitti 图像处理 特征提取的科技树 attilax总结
    Atitit 软件体系的进化,是否需要一个处理中心
    Atitit 项目的主体设计与结构文档 v5
    Atitti 过程导向 vs 结果导向 attilax的策略
    Atitti 过程导向 vs 结果导向 attlax的策
    Atitit 版本管理----分支管理
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/4153142.html
Copyright © 2011-2022 走看看