歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android-簡單音樂播放器

Android-簡單音樂播放器

日期:2017/3/1 10:47:23   编辑:Linux編程

Android-簡單音樂播放器:

//目錄結構


//項目運行結果


//只一個主Activity,裡面代碼也很簡單。至於布局這些就相當簡單了,我就不寫了。

  1. package sn.len.music;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.media.MediaPlayer;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. public class MusicPlayerActivity extends Activity implements OnClickListener
  14. {
  15. private EditText editText;
  16. private MediaPlayer mediaPlayer;
  17. private String fileName=null;
  18. private int postition;
  19. private static final String TAG="MusicPlayerActivity";
  20. @Override
  21. public void onCreate(Bundle savedInstanceState)
  22. {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. mediaPlayer=new MediaPlayer();
  26. editText=(EditText)findViewById(R.id.option);
  27. fileName=editText.getText().toString();
  28. Button start=(Button)findViewById(R.id.start);
  29. Button pause=(Button)findViewById(R.id.pause);
  30. Button replay=(Button)findViewById(R.id.replay);
  31. Button stop=(Button)findViewById(R.id.stop);
  32. start.setOnClickListener(this);
  33. pause.setOnClickListener(this);
  34. replay.setOnClickListener(this);
  35. stop.setOnClickListener(this);
  36. }
  37. @Override
  38. public void onClick(View v)
  39. {
  40. Button continue_but=(Button)v;
  41. try
  42. {
  43. switch (v.getId())
  44. {
  45. case R.id.start://開始
  46. {
  47. play();
  48. }break;
  49. case R.id.pause://暫停
  50. {
  51. //如果正在播放,則停止
  52. if(mediaPlayer.isPlaying())
  53. {
  54. mediaPlayer.pause();
  55. continue_but.setText(R.string.conti);//把暫停設置為繼續播放
  56. }
  57. else//否則繼續播放
  58. {
  59. mediaPlayer.start();
  60. continue_but.setText(R.string.pause);//把繼續播放按鍵設置為暫停
  61. }
  62. }break;
  63. case R.id.replay: // 重播
  64. {
  65. if(mediaPlayer.isPlaying())
  66. {
  67. mediaPlayer.seekTo(0);//重新開始放
  68. }
  69. else
  70. {
  71. play();
  72. }
  73. }
  74. break;
  75. case R.id.stop: //停止
  76. {
  77. if(mediaPlayer.isPlaying())
  78. {
  79. mediaPlayer.stop();
  80. }
  81. }
  82. break;
  83. default:break;
  84. }
  85. }
  86. catch (Exception e)
  87. {
  88. Log.e(TAG, e.toString());
  89. e.printStackTrace();
  90. }
  91. }
  92. private void play() throws IOException
  93. {
  94. //文件在SD卡,所以要弄個File對象
  95. File file=new File(Environment.getExternalStorageDirectory(),fileName);
  96. mediaPlayer.reset();
  97. //設置你要播放的文件在什麼地方
  98. mediaPlayer.setDataSource(file.getAbsolutePath());
  99. Log.i("ABS", file.getAbsolutePath());//得到的結果就為/mnt/sdcard/saybye.mp3
  100. //開始初始化, 渲染
  101. mediaPlayer.prepare();
  102. //開始播放
  103. mediaPlayer.start();
  104. }
  105. @Override
  106. protected void onDestroy()
  107. {
  108. mediaPlayer.release();//釋放掉資源
  109. super.onDestroy();
  110. }
  111. //解決當電話來的時候,使音樂暫停
  112. //當別的Activity來的時候,此Activity肯定會處於暫停狀態。
  113. @Override
  114. protected void onPause()
  115. {
  116. if(mediaPlayer.isPlaying())
  117. {
  118. //記錄下當前音樂播放的位置
  119. postition=mediaPlayer.getCurrentPosition();
  120. //暫停音樂
  121. mediaPlayer.pause();
  122. }
  123. super.onPause();
  124. }
  125. //回到本Activity的時候
  126. @Override
  127. protected void onResume()
  128. {
  129. //如果有設置的斷點,並且文件名不為空,則又開始播放
  130. if(postition>0 && fileName!=null)
  131. {
  132. try
  133. {
  134. play();
  135. mediaPlayer.seekTo(postition); //執行到指定斷點
  136. postition=0;
  137. }
  138. catch (IOException e)
  139. {
  140. e.printStackTrace();
  141. }
  142. }
  143. super.onResume();
  144. }
  145. //解決內存不夠的時候
  146. //當被殺掉了,第二次運行此程序的時候,把上次的值給取出來
  147. @Override
  148. protected void onRestoreInstanceState(Bundle savedInstanceState)
  149. {
  150. postition=savedInstanceState.getInt("postition");
  151. fileName=savedInstanceState.getString("fileName");
  152. super.onRestoreInstanceState(savedInstanceState);
  153. }
  154. //當內存不夠的時候,系統有可能會殺掉某個應用程序
  155. //所以這個時候可以當在殺掉的時候,就可以把數據給保存下來,以供下次使用
  156. @Override
  157. protected void onSaveInstanceState(Bundle outState)
  158. {
  159. //保存下斷點位置以及文件名
  160. outState.putInt("postition", postition);
  161. outState.putString("fileName", fileName);
  162. super.onSaveInstanceState(outState);
  163. }
  164. }
Copyright © Linux教程網 All Rights Reserved