Android之MediaPlayer播放音乐并实现进度条实例

首先,说明我们是从sd卡里读文件,来播放文件!!

1、效果图:


提前工作,往sd卡里放音乐文件,如图:



2、布局文件main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView   
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="歌曲名:"/>  
  10.     <TextView   
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="blueflawer.mp3"/>  
  14.     <Button   
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="播放"  
  18.         android:id="@+id/play_pause"/>  
  19.     <Button   
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="重置"  
  23.         android:id="@+id/reset"/>  
  24.     <SeekBar   
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:id="@+id/seekbar"/>  
  28. </LinearLayout>  
3、activity类
  1. package cn.csdn.activity;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.Timer;  
  6. import java.util.TimerTask;  
  7.   
  8. import android.app.Activity;  
  9. import android.media.MediaPlayer;  
  10. import android.os.Bundle;  
  11. import android.os.Environment;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15. import android.widget.SeekBar;  
  16. import android.widget.SeekBar.OnSeekBarChangeListener;  
  17.   
  18. public class MyPlayerActivity extends Activity {  
  19.     private Button play_pause, reset;  
  20.     private SeekBar seekbar;  
  21.     private boolean ifplay = false;  
  22.     private MediaPlayer player = null;  
  23.     private String musicName = "blueflawer.mp3";  
  24.     private boolean iffirst = false;  
  25.     private Timer mTimer;    
  26.     private TimerTask mTimerTask;   
  27.     private boolean isChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突   
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.         player = new MediaPlayer();  
  32.         findViews();// 各组件  
  33.     }  
  34.   
  35.     private void findViews() {  
  36.         play_pause = (Button) findViewById(R.id.play_pause);  
  37.         reset = (Button) findViewById(R.id.reset);  
  38.         play_pause.setOnClickListener(new MyClick());  
  39.         reset.setOnClickListener(new MyClick());  
  40.   
  41.         seekbar = (SeekBar) findViewById(R.id.seekbar);  
  42.         seekbar.setOnSeekBarChangeListener(new MySeekbar());  
  43.     }  
  44.   
  45.     class MyClick implements OnClickListener {  
  46.         public void onClick(View v) {  
  47.             File file = new File(Environment.getExternalStorageDirectory(),  
  48.                     musicName);  
  49.             // 判断有没有要播放的文件  
  50.             if (file.exists()) {  
  51.                 switch (v.getId()) {  
  52.                 case R.id.play_pause:  
  53.                     if (player != null && !ifplay) {  
  54.                         play_pause.setText("暂停");  
  55.                         if (!iffirst) {  
  56.                             player.reset();  
  57.                             try {  
  58.                                 player.setDataSource(file.getAbsolutePath());  
  59.                                 player.prepare();// 准备  
  60.   
  61.                             } catch (IllegalArgumentException e) {  
  62.                                 e.printStackTrace();  
  63.                             } catch (IllegalStateException e) {  
  64.                                 e.printStackTrace();  
  65.                             } catch (IOException e) {  
  66.                                 e.printStackTrace();  
  67.                             }  
  68.                             seekbar.setMax(player.getDuration());//设置进度条  
  69.                             //----------定时器记录播放进度---------//    
  70.                             mTimer = new Timer();    
  71.                             mTimerTask = new TimerTask() {    
  72.                                 @Override    
  73.                                 public void run() {     
  74.                                     if(isChanging==true) {   
  75.                                         return;    
  76.                                     }  
  77.                                     seekbar.setProgress(player.getCurrentPosition());  
  78.                                 }    
  79.                             };   
  80.                             mTimer.schedule(mTimerTask, 010);   
  81.                             iffirst=true;  
  82.                         }  
  83.                         player.start();// 开始  
  84.                         ifplay = true;  
  85.                     } else if (ifplay) {  
  86.                         play_pause.setText("继续");  
  87.                         player.pause();  
  88.                         ifplay = false;  
  89.                     }  
  90.                     break;  
  91.                 case R.id.reset:  
  92.                     if (ifplay) {  
  93.                         player.seekTo(0);  
  94.                     } else {  
  95.                         player.reset();  
  96.                         try {  
  97.                             player.setDataSource(file.getAbsolutePath());  
  98.                             player.prepare();// 准备  
  99.                             player.start();// 开始  
  100.                         } catch (IllegalArgumentException e) {  
  101.                             e.printStackTrace();  
  102.                         } catch (IllegalStateException e) {  
  103.                             e.printStackTrace();  
  104.                         } catch (IOException e) {  
  105.                             e.printStackTrace();  
  106.                         }  
  107.                     }  
  108.                     break;  
  109.                 }  
  110.             }  
  111.         }  
  112.     }  
  113.     //进度条处理  
  114.     class MySeekbar implements OnSeekBarChangeListener {  
  115.         public void onProgressChanged(SeekBar seekBar, int progress,  
  116.                 boolean fromUser) {  
  117.         }  
  118.   
  119.         public void onStartTrackingTouch(SeekBar seekBar) {  
  120.             isChanging=true;    
  121.         }  
  122.   
  123.         public void onStopTrackingTouch(SeekBar seekBar) {  
  124.             player.seekTo(seekbar.getProgress());  
  125.             isChanging=false;    
  126.         }  
  127.   
  128.     }  
  129.     //来电处理  
  130.     protected void onDestroy() {  
  131.         if(player != null){  
  132.             if(player.isPlaying()){  
  133.                 player.stop();  
  134.             }  
  135.             player.release();  
  136.         }  
  137.         super.onDestroy();  
  138.     }  
  139.   
  140.     protected void onPause() {  
  141.         if(player != null){  
  142.             if(player.isPlaying()){  
  143.                 player.pause();  
  144.             }  
  145.         }  
  146.         super.onPause();  
  147.     }  
  148.   
  149.     protected void onResume() {  
  150.         if(player != null){  
  151.             if(!player.isPlaying()){  
  152.                 player.start();  
  153.             }  
  154.         }  
  155.         super.onResume();  
  156.     }  
  157.   

发布了287 篇原创文章 · 获赞 207 · 访问量 174万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览