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

一個簡單的Android音樂播放器

日期:2017/3/1 10:44:38   编辑:Linux編程

實現功能,播放,暫停,重置,進度條的使用

String文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">MusicPlayer</string>
  4. <string name="music_name">歌曲:</string>
  5. <string name="play_text">播放</string>
  6. <string name="pause_text">暫停</string>
  7. <string name="continue_text">繼續</string>
  8. <string name="reset_text">重置</string>
  9. <string name="stop_text">關閉</string>
  10. <string name="notfoundfile_text">媒體文件不存在</string>
  11. <string name="notfoundSdcard_text">SDCard不存在</string>
  12. </resources>

布局:

  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="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/music_name" />
  10. <EditText
  11. android:id="@+id/musicEt"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="a.mp3"
  15. />
  16. <SeekBar
  17. android:id="@+id/seekBar"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:max="10000"
  21. />
  22. <TableLayout
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:stretchColumns="*" >
  26. <TableRow >
  27. <Button
  28. android:id="@+id/playBtn"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@string/play_text" />
  32. <Button
  33. android:id="@+id/pauseBtn"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="@string/pause_text" />
  37. <Button
  38. android:id="@+id/resetBtn"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:text="@string/reset_text" />
  42. <Button
  43. android:id="@+id/stopBtn"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:text="@string/stop_text" />
  47. </TableRow>
  48. </TableLayout>
  49. </LinearLayout>

Java源碼:

  1. package cn.class3g.musicplayer;
  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.os.Handler;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.SeekBar;
  15. import android.widget.SeekBar.OnSeekBarChangeListener;
  16. import android.widget.Toast;
  17. public class MusicPlayerActivity extends Activity implements OnClickListener,
  18. OnSeekBarChangeListener {
  19. private static final String TAG = "MusicPlayerActivity";
  20. EditText musicEt;
  21. Button playBtn, pauseBtn, resetBtn, stopBtn;
  22. Handler handler = null;
  23. Handler fHandler = null;
  24. MediaPlayer player = null;
  25. SeekBar seekbar = null;
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. player = new MediaPlayer();
  30. findViews();
  31. }
  32. private void findViews() {
  33. musicEt = (EditText) this.findViewById(R.id.musicEt);
  34. playBtn = (Button) this.findViewById(R.id.playBtn);
  35. pauseBtn = (Button) this.findViewById(R.id.pauseBtn);
  36. resetBtn = (Button) this.findViewById(R.id.resetBtn);
  37. stopBtn = (Button) this.findViewById(R.id.stopBtn);
  38. seekbar = (SeekBar) this.findViewById(R.id.seekBar);
  39. playBtn.setOnClickListener(this);
  40. pauseBtn.setOnClickListener(this);
  41. resetBtn.setOnClickListener(this);
  42. stopBtn.setOnClickListener(this);
  43. seekbar.setOnSeekBarChangeListener(this);
  44. }
  45. public void onClick(View v) {
  46. String fileName = musicEt.getText().toString();
  47. // 確定sdcard狀態是否為已加載
  48. if (Environment.getExternalStorageState().equals(
  49. Environment.MEDIA_MOUNTED)) {
  50. File file = new File(Environment.getExternalStorageDirectory(),
  51. fileName);
  52. // 判斷所播放的媒體文件是否存在
  53. if (file.exists()) {
  54. try {
  55. switch (v.getId()) {
  56. case R.id.playBtn:
  57. playMusic(file);
  58. break;
  59. case R.id.pauseBtn:
  60. if (player.isPlaying()) {
  61. player.pause();
  62. pauseBtn.setText(R.string.continue_text);
  63. } else {
  64. player.start();
  65. pauseBtn.setText(R.string.pause_text);
  66. }
  67. break;
  68. case R.id.resetBtn:
  69. if (player.isPlaying()) {
  70. player.seekTo(0);
  71. } else {
  72. playMusic(file);
  73. }
  74. break;
  75. case R.id.stopBtn:
  76. if (player.isPlaying()) {
  77. player.stop();
  78. }
  79. break;
  80. }
  81. } catch (Exception e) {
  82. Log.e(TAG, e.toString());
  83. }
  84. } else {
  85. Toast.makeText(this, R.string.notfoundfile_text,
  86. Toast.LENGTH_SHORT).show();
  87. }
  88. } else {
  89. Toast.makeText(this, R.string.notfoundSdcard_text,
  90. Toast.LENGTH_SHORT).show();
  91. }
  92. }
  93. protected void onDestroy() {
  94. if (player != null) {
  95. if (player.isPlaying()) {
  96. player.stop();
  97. }
  98. player.release();
  99. }
  100. super.onDestroy();
  101. }
  102. protected void onPause() {
  103. if (player != null) {
  104. if (player.isPlaying()) {
  105. player.pause();
  106. }
  107. }
  108. super.onPause();
  109. }
  110. protected void onResume() {
  111. if (player != null) {
  112. if (!player.isPlaying()) {
  113. player.start();
  114. }
  115. }
  116. super.onResume();
  117. }
  118. private void playMusic(File file) throws IllegalArgumentException,
  119. IllegalStateException, IOException {
  120. if (player != null) {
  121. player.reset();
  122. player.setDataSource(file.getAbsolutePath());
  123. player.prepare();
  124. player.start();
  125. seekbar.setMax(player.getDuration());
  126. Log.i(TAG, String.valueOf(player.getDuration()));
  127. new Thread(new seekBar()).start();
  128. }
  129. }
  130. class seekBar implements Runnable {
  131. int position = 0;
  132. public void run() {
  133. while (player != null) {
  134. position = player.getCurrentPosition();
  135. seekbar.setProgress(position);
  136. try {
  137. Thread.sleep(100);
  138. } catch (InterruptedException e) {
  139. Log.e(TAG, e.toString());
  140. }
  141. }
  142. }
  143. }
  144. public void onProgressChanged(SeekBar seekBar, int progress,
  145. boolean fromUser) {
  146. if (fromUser)
  147. player.seekTo(progress);
  148. }
  149. public void onStartTrackingTouch(SeekBar seekBar) {
  150. }
  151. public void onStopTrackingTouch(SeekBar seekBar) {
  152. }
  153. }
Copyright © Linux教程網 All Rights Reserved