Android音乐播放器(雏形)


建议:学习本实例之前,请掌握Activity的生命周期相关的事件和方法,这样学习效果会更好。 

本实例仅供参考学习,并非一款非常完善的产品。由于时间和本人技术有限,不足或者错误之处敬请谅解。希望热心的网友能够继续完善。

相关阅读:

Android中的Activity生命周期

Android Activity生命周期以及LoCat的使用

下面是Activity部分代码(我一般都会有详细注释):

  1. package cn.chaoyang.activity;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Activity;  
  7. import android.media.MediaPlayer;  
  8. import android.os.Bundle;  
  9. import android.os.Environment;  
  10. import android.text.BoringLayout.Metrics;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13. import android.widget.EditText;  
  14. //学习本实例之前,请掌握Activity的生命周期和相关的方法,这样学习效果会更好。   
  15. public class MainActivity extends Activity {  
  16.     private MediaPlayer mediaplayer;  
  17.     private EditText txtName;  
  18.     private int postion;  
  19.     private String fileName;  
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         ButtonClickListener listener =new ButtonClickListener();  
  26.          txtName =(EditText)this.findViewById(R.id.inputName);  
  27.         Button btnPlay =(Button)this.findViewById(R.id.btnPlay);  
  28.         Button btnPause =(Button) this.findViewById(R.id.btnPause);  
  29.         Button btnStop =(Button) this.findViewById(R.id.btnStop);  
  30.         Button btnResart=(Button) this.findViewById(R.id.btnRestart);  
  31.         btnPlay.setOnClickListener(listener);  
  32.         btnPause.setOnClickListener(listener);  
  33.         btnStop.setOnClickListener(listener);  
  34.         btnResart.setOnClickListener(listener);  
  35.     }  
  36.     //当系统恢复后,可以重新读取出之前保存的状态值   
  37.     @Override  
  38.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  39.       this.fileName=savedInstanceState.getString("fileName");  
  40.       this.postion=savedInstanceState.getInt("postion");  
  41.       super.onRestoreInstanceState(savedInstanceState);  
  42.     }  
  43.   //当发生意外时,在系统将Activity的进程杀死之前,保存一些状态值   
  44.     @Override  
  45.     protected void onSaveInstanceState(Bundle outState) {  
  46.         outState.putString("fileName", fileName);  
  47.         outState.putInt("postion",postion);  
  48.         super.onSaveInstanceState(outState);  
  49.     }  
  50.   //onDestroy方法可以杀掉程序的进程,彻底释放资源   
  51.     @Override  
  52.     protected void onDestroy() {  
  53.         mediaplayer.release();  
  54.         super.onDestroy();  
  55.     }  
  56.    //如果打电话结束了,继续播放音乐   
  57.     @Override  
  58.     protected void onResume() {  
  59.         if(postion>0&&fileName!=null)  
  60.         {  
  61.             try {  
  62.                 play();  
  63.             } catch (IOException e) {  
  64.                 // TODO Auto-generated catch block   
  65.                 e.printStackTrace();  
  66.             }  
  67.             mediaplayer.seekTo(postion);  
  68.             postion=0;  
  69.         }  
  70.         super.onResume();  
  71.     }  
  72.   //如果突然来电话或者来短信,Acticity会暂停,停止播放音乐   
  73.     @Override  
  74.     protected void onPause() {  
  75.         if(mediaplayer.isPlaying())  
  76.         {  
  77.             postion =mediaplayer.getCurrentPosition();//保存当前播放点   
  78.             mediaplayer.stop();  
  79.         }  
  80.         super.onPause();  
  81.     }  
  82.   
  83.     private final class ButtonClickListener implements View.OnClickListener  
  84.     {  
  85.                
  86.         @Override  
  87.         public void onClick(View v) {  
  88.             // TODO Auto-generated method stub   
  89.             mediaplayer =new MediaPlayer();  
  90.               
  91.             Button button =(Button) v ;  
  92.             try {  
  93.             switch (v.getId())  
  94.             {  
  95.             //播放   
  96.             case R.id.btnPlay:  
  97.                 if(!mediaplayer.isPlaying())  
  98.                 {  
  99.             play();  
  100.                 }  
  101.             break;  
  102.             //暂停   
  103.             case R.id.btnPause:  
  104.                 //如果正在播放,则按下按钮后暂停.且按钮上的文本显示为"继续“   
  105.                 if(mediaplayer.isPlaying())  
  106.                 {  
  107.                     mediaplayer.pause();  
  108.                     button.setText(R.string.txtContinue);//设置按钮文本   
  109.                 }else{  
  110.                     //如果是暂停状态,按下按钮后继续播放   
  111.                    //play();   
  112.                 }  
  113.                 break;  
  114.                 //停止   
  115.             case R.id.btnStop:  
  116.                 if(mediaplayer.isPlaying()){  
  117.                 mediaplayer.stop();  
  118.                 }  
  119.                 break;  
  120.                   
  121.                 //重复   
  122.             case R.id.btnRestart:  
  123.                 if(mediaplayer.isPlaying()){  
  124.                     mediaplayer.seekTo(0);  
  125.                 }else{  
  126.                     play();  
  127.                 }  
  128.                 break;  
  129.             }  
  130.             }catch (Exception e) {  
  131.                 // TODO: handle exception   
  132.             }  
  133.               
  134.               
  135.         }  
  136.   
  137.     }  
  138.     private void play() throws IOException  
  139.     {  
  140.         //获得音乐文件的绝对路径   
  141.          fileName=txtName.getText().toString();  
  142.         File file =new File(Environment.getExternalStorageDirectory(),fileName);  
  143.         mediaplayer.reset();//归位   
  144.         mediaplayer.setDataSource(file.getAbsolutePath());//设置需要播放的数据源   
  145.         mediaplayer.prepare();  
  146.         mediaplayer.start();  
  147.     }  
  148. }  
  • 1
  • 2
  • 下一页

相关内容