MediaPlayer 之音乐循环播放器


  先说以下开发环境:Android sdk 2.2   target:android-8

 

AndroidManifest.xml:

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.icox.android"  
  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="MusicPlayer"  
  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.     <!-- WRITE_EXTERNAL_STORAGE sd 卡的权限 -->  
  19.     <uses-permission android:name="WRITE_EXTERNAL_STORAGE"></uses-permission>  
  20. </manifest>  

 

 

/res/layout/main.xml:

 

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:id="@+id/mTextView" android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:text=""></TextView>  
  7.     <LinearLayout android:id="@+id/ll_list"  
  8.         android:orientation="vertical" android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">  
  10.         <ListView android:id="@+id/lv_music" android:layout_width="fill_parent"  
  11.             android:layout_height="wrap_content"></ListView>  
  12.     </LinearLayout>  
  13.   
  14.     <LinearLayout android:id="@+id/ll_btn"  
  15.         android:orientation="horizontal" android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content">  
  17.         <ImageButton android:id="@+id/imgbtn_loop" android:src="@drawable/loop"  
  18.             android:background="#00000000" android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content" />  
  20.   
  21.         <ImageButton android:id="@+id/imgbtn_last" android:src="@drawable/last"  
  22.             android:background="#00000000" android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"></ImageButton>  
  24.         <ImageButton android:id="@+id/imgbtn_start" android:src="@drawable/start"  
  25.             android:background="#00000000" android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"></ImageButton>  
  27.               
  28.         <ImageButton android:id="@+id/imgbtn_pause" android:src="@drawable/pause"  
  29.             android:background="#00000000" android:visibility="gone"  
  30.             android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>  
  31.   
  32.         <ImageButton android:id="@+id/imgbtn_stop" android:src="@drawable/stop"  
  33.             android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>  
  34.         <ImageButton android:id="@+id/imgbtn_next" android:src="@drawable/next"  
  35.             android:background="#00000000" android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"></ImageButton>  
  37.   
  38.     </LinearLayout>  
  39.   
  40.     <LinearLayout android:id="@+id/ll_btn"  
  41.         android:orientation="vertical" android:layout_width="fill_parent"  
  42.         android:layout_height="wrap_content">  
  43.     </LinearLayout>  
  44.     <SeekBar android:layout_height="wrap_content" android:id="@+id/SeekBar01"  
  45.         android:layout_width="fill_parent"></SeekBar>  
  46.   
  47. </LinearLayout>  

 

    /res/drawable 目录下放需要的图片资源文件。skip...

 

 

 

 

 

MusicPlayer.java:

 

Java代码   收藏代码
  1. package com.icox.android;  
  2. import java.io.File;  
  3. import java.io.FilenameFilter;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.content.DialogInterface;  
  9. import android.media.MediaPlayer;  
  10. import android.media.MediaPlayer.OnCompletionListener;  
  11. import android.media.MediaPlayer.OnErrorListener;  
  12. import android.os.Bundle;  
  13. import android.os.Handler;  
  14. import android.os.Message;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17. import android.widget.AdapterView;  
  18. import android.widget.ArrayAdapter;  
  19. import android.widget.ImageButton;  
  20. import android.widget.ListView;  
  21. import android.widget.SeekBar;  
  22. import android.widget.TextView;  
  23.   
  24.   
  25. public class MusicPlayer extends Activity implements Runnable {  
  26.     /** Called when the activity is first created. */  
  27.     private MediaPlayer MediaPlayer = null;  
  28.     private SeekBar SeekBar = null;  
  29.     private ListView ListView = null;  
  30.     private ImageButton btnLast = null;  
  31.     private ImageButton btnStart = null;  
  32.     private ImageButton btnPause = null;  
  33.     private ImageButton btnStop = null;  
  34.     private ImageButton btnNext = null;  
  35.     private ImageButton btnLoop = null;  
  36.     private TextView TextView = null;  
  37.     private List<String> MusicList = null;  
  38.     private int Current = 0;  
  39.     private int count = 0;  
  40.     private boolean isrun = false;  
  41.     private boolean isauto = false;  
  42.     private static final String PATH = "/sdcard/";  
  43.   
  44.     @Override  
  45.     public void onCreate(Bundle savedInstanceState) {  
  46.         super.onCreate(savedInstanceState);  
  47.         // setContentView(R.layout.music);  
  48.         setContentView(R.layout.main);  
  49.         ListView = (ListView) this.findViewById(R.id.lv_music);  
  50.         SeekBar = (SeekBar) this.findViewById(R.id.SeekBar01);  
  51.         TextView = (TextView) this.findViewById(R.id.mTextView);  
  52.         TextView.setText("Louisa 的迷你音乐播放器");  
  53.         btnLoop = (ImageButton) findViewById(R.id.imgbtn_loop);  
  54.         btnLast = (ImageButton) this.findViewById(R.id.imgbtn_last);  
  55.         btnStart = (ImageButton) this.findViewById(R.id.imgbtn_start);  
  56.         btnPause = (ImageButton) this.findViewById(R.id.imgbtn_pause);  
  57.         btnStop = (ImageButton) this.findViewById(R.id.imgbtn_stop);  
  58.         btnNext = (ImageButton) this.findViewById(R.id.imgbtn_next);  
  59.         MusicList = new ArrayList<String>();  
  60.         MediaPlayer = new MediaPlayer();  
  61.         // 循环按钮  
  62.         btnLoop.setOnClickListener(new ImageButton.OnClickListener() {  
  63.   
  64.             public void onClick(View v) {  
  65.                 LoopMusic();  
  66.             }  
  67.   
  68.         });  
  69.   
  70.         // 开始按钮  
  71.         btnStart.setOnClickListener(new ImageButton.OnClickListener() {  
  72.   
  73.             @Override  
  74.             public void onClick(View v) {  
  75.                 PlayMusic(PATH + MusicList.get(Current));  
  76.             }  
  77.   
  78.         });  
  79.         // 下一首  
  80.         btnNext.setOnClickListener(new ImageButton.OnClickListener() {  
  81.   
  82.             @Override  
  83.             public void onClick(View v) {  
  84.                 // TODO Auto-generated method stub  
  85.                 NextMusic();  
  86.             }  
  87.   
  88.         });  
  89.         // 上一首  
  90.         btnLast.setOnClickListener(new ImageButton.OnClickListener() {  
  91.   
  92.             @Override  
  93.             public void onClick(View v) {  
  94.                 // TODO Auto-generated method stub  
  95.                 LastMusic();  
  96.             }  
  97.   
  98.         });  
  99.         // 暂停  
  100.         btnPause.setOnClickListener(new ImageButton.OnClickListener() {  
  101.   
  102.             @Override  
  103.             public void onClick(View v) {  
  104.                 // TODO Auto-generated method stub  
  105.                 isrun = true;  
  106.                 isauto = false;  
  107.                 btnStart.setVisibility(View.VISIBLE);// 显示启动按钮  
  108.                 btnPause.setVisibility(View.GONE);// 隐藏暂停按钮  
  109.   
  110.                 // 是否正在播放  
  111.                 if (MediaPlayer.isPlaying()) {  
  112.                     MediaPlayer.pause();  
  113.                 }  
  114.             }  
  115.   
  116.         });  
  117.         // 停止  
  118.         btnStop.setOnClickListener(new ImageButton.OnClickListener() {  
  119.   
  120.             @Override  
  121.             public void onClick(View v) {  
  122.                 // TODO Auto-generated method stub  
  123.                 StopMusic();  
  124.             }  
  125.   
  126.         });  
  127.   
  128.         // 单击音乐播放列表,播放歌曲事件  
  129.         ListView.setOnItemClickListener(new ListView.OnItemClickListener() {  
  130.   
  131.             @Override  
  132.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  133.                     long arg3) {  
  134.                 Current = arg2;  
  135.                 StopMusic();  
  136.                 PlayMusic(PATH + MusicList.get(Current));  
  137.             }  
  138.   
  139.         });  
  140.   
  141.         SeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {  
  142.   
  143.             @Override  
  144.             public void onProgressChanged(SeekBar seekBar, int progress,  
  145.                     boolean fromUser) {  
  146.                 if (!isauto) {  
  147.                     // int n = mMediaPlayer.getCurrentPosition();  
  148.                     // mMediaPlayer.pause();  
  149.                     MediaPlayer.seekTo(progress);  
  150.                     // mMediaPlayer.start();  
  151.                     // isauto = true;  
  152.                 }  
  153.   
  154.             }  
  155.   
  156.             @Override  
  157.             public void onStartTrackingTouch(SeekBar seekBar) {  
  158.                 isauto = false;  
  159.             }  
  160.   
  161.             @Override  
  162.             public void onStopTrackingTouch(SeekBar seekBar) {  
  163.                 isauto = true;  
  164.             }  
  165.   
  166.         });  
  167.         this.MusicList();  
  168.         new Thread(this).start();  
  169.     }  
  170.   
  171.     public void PlayMusic(String path) {  
  172.   
  173.         try {  
  174.             btnStart.setVisibility(View.GONE);// 隐藏启动按钮  
  175.             btnPause.setVisibility(View.VISIBLE);// 显示暂停按钮  
  176.             if (!isrun) {  
  177.                 MediaPlayer.reset();// 重置  
  178.                 MediaPlayer.setDataSource(path);// 设置数据源  
  179.                 MediaPlayer.prepare();// 准备  
  180.                 MediaPlayer.start();// 开始播放  
  181.                 count = MediaPlayer.getDuration();  
  182.                 Log.i("TAG-count", count + "");  
  183.                 SeekBar.setMax(count);// 设置最大值.  
  184.                 this.TextView.setText("当前播放歌曲:" + MusicList.get(Current));  
  185.                 MediaPlayer  
  186.                         .setOnCompletionListener(new OnCompletionListener() {  
  187.   
  188.                             @Override  
  189.                             public void onCompletion(MediaPlayer mp) {  
  190.                                 // TODO Auto-generated method stub  
  191.                                 NextMusic();  
  192.                             }  
  193.   
  194.                         });  
  195.                 MediaPlayer.setOnErrorListener(new OnErrorListener() {  
  196.   
  197.                     @Override  
  198.                     public boolean onError(MediaPlayer mp, int what, int extra) {  
  199.                         ShowDialog("Error");  
  200.                         MediaPlayer.reset();  
  201.                         return true;  
  202.                     }  
  203.   
  204.                 });  
  205.   
  206.             } else {  
  207.                 MediaPlayer.start();// 暂停之后接着播放  
  208.             }  
  209.             isauto = true;  
  210.         } catch (Exception ex) {  
  211.             this.ShowDialog("播放音乐异常:" + ex.getMessage());  
  212.         }  
  213.     }  
  214.   
  215.     public void LoopMusic(){  
  216.         isrun=true;  
  217.         if(btnLoop.isPressed()==true  && MediaPlayer.isPlaying()){  
  218.             MediaPlayer.setLooping(true);  
  219.             MediaPlayer.start();  
  220.         }  
  221.       
  222.     }  
  223.       
  224.   
  225.       
  226.     public void NextMusic() {  
  227.         int num = MusicList.size();  
  228.         if (++Current >= num) {  
  229.             Current = 0;  
  230.         }  
  231.         StopMusic();  
  232.         PlayMusic(PATH + MusicList.get(Current));  
  233.   
  234.     }  
  235.   
  236.     public void LastMusic() {  
  237.         int num = MusicList.size();  
  238.         if (--Current < 0) {  
  239.             Current = num - 1;  
  240.         }  
  241.         StopMusic();  
  242.         PlayMusic(PATH + MusicList.get(Current));  
  243.   
  244.     }  
  245.   
  246.     @Override  
  247.     protected void onPause() {  
  248.         super.onPause();  
  249.         isauto = false;  
  250.         if (MediaPlayer.isPlaying()) {  
  251.             MediaPlayer.stop();// 停止  
  252.         }  
  253.         MediaPlayer.reset();  
  254.         MediaPlayer.release();  
  255.         // mMediaPlayer = null;  
  256.     }  
  257.   
  258.     public void StopMusic() {  
  259.         isrun = false;  
  260.         // isauto = false;  
  261.         btnPause.setVisibility(View.GONE);// 隐藏暂停按钮  
  262.         btnStart.setVisibility(View.VISIBLE);// 显示启动按钮  
  263.   
  264.         if (MediaPlayer.isPlaying()) {  
  265.             MediaPlayer.stop();// 停止  
  266.         }  
  267.         // mSeekBar.setProgress(0);  
  268.     }  
  269.   
  270.   
  271.   
  272.     // 关于循环播放的设置:  
  273.     // if (Common.PLAY_MODE_SINGLE_LOOP == mPlayMode) {  
  274.     // mMediaPlayer.setLooping(true); // 单曲循环  
  275.     // } else {  
  276.     // mMediaPlayer.setLooping(false); // 不循环播放  
  277.     // }  
  278.     // mMediaPlayer.start(); // 开始播放  
  279.       
  280.       
  281.   
  282.     /** 
  283.      * 文件过滤器 
  284.      *  
  285.      * @author Louisa.Smart 
  286.      *  
  287.      */  
  288.     class MusicFilter implements FilenameFilter {  
  289.   
  290.     @Override  
  291.     public boolean accept(File dir, String filename) {  
  292.         // TODO Auto-generated method stub  
  293.         return (filename.endsWith(".mp3"));  
  294.     }  
  295.   
  296.   
  297.     }  
  298.   
  299.     /** 
  300.      * 播放列表 
  301.      */  
  302.     public void MusicList() {  
  303.         try {  
  304.             File home = new File(PATH);  
  305.             File[] f = home.listFiles(new MusicFilter());  
  306.             if (f.length > 0) {  
  307.                 for (int i = 0; i < f.length; i++) {  
  308.                     File file = f[i];  
  309.                     MusicList.add(file.getName().toString());  
  310.                 }  
  311.                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  312.                         android.R.layout.simple_list_item_1, MusicList);  
  313.                 ListView.setAdapter(adapter);  
  314.             }  
  315.         } catch (Exception ex) {  
  316.             this.ShowDialog("显示音乐列表异常:" + ex.getMessage());  
  317.         }  
  318.   
  319.     }  
  320.   
  321.     public void ShowDialog(String str) {  
  322.         new AlertDialog.Builder(this).setTitle("提示").setMessage(str)  
  323.                 .setPositiveButton("OK"new DialogInterface.OnClickListener() {  
  324.   
  325.                     @Override  
  326.                     public void onClick(DialogInterface dialog, int which) {  
  327.                     }  
  328.   
  329.                 }).show();  
  330.     }  
  331.   
  332.     @Override  
  333.     public void run() {  
  334.         // TODO Auto-generated method stub  
  335.         while (true) {  
  336.             try {  
  337.                 if (isauto) {  
  338.                     int n = MediaPlayer.getCurrentPosition();  
  339.                     Message msg = new Message();  
  340.                     msg.what = n;  
  341.                     handler.sendMessage(msg);  
  342.                 }  
  343.                 Thread.sleep(100);  
  344.             } catch (Exception ex) {  
  345.                 ex.printStackTrace();  
  346.             }  
  347.   
  348.         }  
  349.     }  
  350.   
  351.     public Handler handler = new Handler() {  
  352.         public void handleMessage(Message msg) {  
  353.             super.handleMessage(msg);  
  354.             SeekBar.setProgress(msg.what);  
  355.             SeekBar.invalidate();  
  356.         }  
  357.     };  
  358.   
  359. }  
  360.       
  361.       

 

 

   run as android application 的时候请选择有sdcard 支持的模拟器,若放在真机里运行,请先确定设备是否是root 权限。

 

 

截图如下所示:

 

 



 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值