音乐播放器的实现

strings.xml

view plaincopy to clipboardprint?

  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="choose_text">选择</string>
  11. <string name="notfoundfile_text">媒体文件不存在</string>
  12. <string name="notfoundSdcard_text">SDcard不存在</string>
  13. </resources>

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MusicPlayer</string> <string name="music_name">曲目</string> <string name="play_text">播放</string> <string name="pause_text">暂停</string> <string name="continue_text">继续</string> <string name="reset_text">重置</string> <string name="stop_text">停止</string> <string name="choose_text">选择</string> <string name="notfoundfile_text">媒体文件不存在</string> <string name="notfoundSdcard_text">SDcard不存在</string> </resources>

main.xml

view plaincopy to clipboardprint?

  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. <TableLayout
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:stretchColumns="0" >
  14. <TableRow >
  15. <EditText
  16. android:id="@+id/musicEt"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="l.mp3" />
  20. <Button
  21. android:id="@+id/chooseBtn"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="@string/choose_text" />
  25. </TableRow>
  26. </TableLayout>
  27. <SeekBar
  28. android:id="@+id/seekBar"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content" />
  31. <SeekBar
  32. android:id="@+id/seekBarSound"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:max="100"
  36. android:progress="10"/>
  37. />
  38. <TextView
  39. android:id="@+id/time"
  40. android:layout_width="fill_parent"
  41. android:layout_height="wrap_content"
  42. />
  43. <TableLayout
  44. android:layout_width="fill_parent"
  45. android:layout_height="wrap_content"
  46. android:stretchColumns="*" >
  47. <TableRow >
  48. <Button
  49. android:id="@+id/playBtn"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="@string/play_text" />
  53. <Button
  54. android:id="@+id/pauseBtn"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:text="@string/pause_text" />
  58. <Button
  59. android:id="@+id/stopBtn"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="@string/stop_text" />
  63. </TableRow>
  64. </TableLayout>
  65. </LinearLayout>

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/music_name" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0" > <TableRow > <EditText android:id="@+id/musicEt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="l.mp3" /> <Button android:id="@+id/chooseBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choose_text" /> </TableRow> </TableLayout> <SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <SeekBar android:id="@+id/seekBarSound" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="10"/> /> <TextView android:id="@+id/time" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" > <TableRow > <Button android:id="@+id/playBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play_text" /> <Button android:id="@+id/pauseBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause_text" /> <Button android:id="@+id/stopBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop_text" /> </TableRow> </TableLayout> </LinearLayout>

MusicPlayerActivity.java

view plaincopy to clipboardprint?

  1. package cn.csdn.playle;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.media.AudioManager;
  8. import android.media.MediaPlayer;
  9. import android.os.Bundle;
  10. import android.os.Environment;
  11. import android.os.Handler;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.SeekBar;
  18. import android.widget.SeekBar.OnSeekBarChangeListener;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21. public class MusicPlayerActivity extends Activity implements OnClickListener,
  22. OnSeekBarChangeListener {
  23. EditText musicEt;
  24. Button playBtn, pauseBtn, stopBtn, chooseBtn;
  25. TextView time;
  26. AudioManager audiomanage;
  27. int maxVolume, currentVolume;
  28. SeekBar seekBar, seekBarSound;
  29. MediaPlayer player = null;
  30. File file = null;
  31. int position = 0;
  32. int i = 0;
  33. Handler handler = new Handler();
  34. public void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.main);
  37. player = new MediaPlayer();
  38. findViews();
  39. }
  40. private void findViews() {
  41. musicEt = (EditText) this.findViewById(R.id.musicEt);
  42. playBtn = (Button) this.findViewById(R.id.playBtn);
  43. pauseBtn = (Button) this.findViewById(R.id.pauseBtn);
  44. stopBtn = (Button) this.findViewById(R.id.stopBtn);
  45. chooseBtn = (Button) this.findViewById(R.id.chooseBtn);
  46. seekBar = (SeekBar) this.findViewById(R.id.seekBar);
  47. seekBarSound = (SeekBar) this.findViewById(R.id.seekBarSound);
  48. time = (TextView) this.findViewById(R.id.time);
  49. time.setOnClickListener(this);
  50. playBtn.setOnClickListener(this);
  51. pauseBtn.setOnClickListener(this);
  52. stopBtn.setOnClickListener(this);
  53. stopBtn.setEnabled(false);
  54. pauseBtn.setEnabled(false);
  55. seekBar.setOnSeekBarChangeListener(this);
  56. chooseBtn.setOnClickListener(new OnClickListener() {
  57. public void onClick(View v) {
  58. Intent intent = new Intent(MusicPlayerActivity.this,
  59. MusicListActivity.class);
  60. startActivity(intent);
  61. }
  62. });
  63. audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  64. maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量
  65. seekBarSound.setMax(maxVolume); // 拖动条最高值与系统最大声匹配
  66. currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
  67. seekBarSound.setProgress(currentVolume);
  68. seekBarSound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() // 调音监听器
  69. {
  70. public void onProgressChanged(SeekBar arg0, int progress,
  71. boolean fromUser) {
  72. audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC,
  73. progress, 0);
  74. currentVolume = audiomanage
  75. .getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
  76. seekBarSound.setProgress(currentVolume);
  77. }
  78. public void onStartTrackingTouch(SeekBar seekBar) {
  79. }
  80. public void onStopTrackingTouch(SeekBar seekBar) {
  81. }
  82. });
  83. Intent intent = this.getIntent();
  84. String name = intent.getStringExtra("name");
  85. musicEt.setText(name);
  86. }
  87. public void onClick(View v) {
  88. String fileName = musicEt.getText().toString().trim();
  89. // 获取sdcard的状态是否已加载 (MEDIA_MOUNTED 加载状态)
  90. if (Environment.getExternalStorageState().equals(
  91. Environment.MEDIA_MOUNTED)) {
  92. file = new File(Environment.getExternalStorageDirectory(), fileName);
  93. // 判断所播放的媒体文件是否存在
  94. if (file.exists()) {
  95. try {
  96. switch (v.getId()) {// 返回一个int值
  97. case R.id.playBtn:
  98. playMusic(file);
  99. break;
  100. case R.id.pauseBtn:
  101. if (player.isPlaying()) {
  102. player.pause();
  103. pauseBtn.setText(R.string.continue_text);
  104. } else {
  105. player.start();
  106. pauseBtn.setText(R.string.pause_text);
  107. }
  108. break;
  109. case R.id.stopBtn:
  110. player.stop();
  111. stopBtn.setEnabled(false);
  112. seekBar.setProgress(0);
  113. if (playBtn.getText().toString().equals("重置")) {
  114. playBtn.setText(R.string.play_text);
  115. }
  116. if (pauseBtn.getText().toString().equals("继续")) {
  117. pauseBtn.setText(R.string.pause_text);
  118. pauseBtn.setEnabled(false);
  119. } else {
  120. pauseBtn.setEnabled(false);
  121. }
  122. break;
  123. }
  124. } catch (IllegalArgumentException e) {
  125. Log.e("TAG", e.toString());
  126. } catch (IllegalStateException e) {
  127. Log.e("TAG", e.toString());
  128. } catch (IOException e) {
  129. Log.e("TAG", e.toString());
  130. }
  131. } else {
  132. Toast.makeText(this, R.string.notfoundfile_text,
  133. Toast.LENGTH_LONG).show();
  134. }
  135. }
  136. else {
  137. Toast.makeText(this, R.string.notfoundSdcard_text,
  138. Toast.LENGTH_LONG).show();
  139. }
  140. }
  141. protected void onDestroy() {
  142. if (player != null) {
  143. if (player.isPlaying()) {
  144. player.stop();
  145. }
  146. player.release();
  147. }
  148. super.onDestroy();
  149. }
  150. protected void onPause() {
  151. if (player != null) {
  152. if (player.isPlaying()) {
  153. player.pause();
  154. }
  155. }
  156. super.onPause();
  157. }
  158. private void playMusic(File file) throws IllegalStateException, IOException {
  159. if (musicEt.getText().toString() == null
  160. || "".equals(musicEt.getText().toString())) {
  161. Toast.makeText(this, "没有选中歌曲", Toast.LENGTH_LONG).show();
  162. } else {
  163. if (player != null) {
  164. pauseBtn.setEnabled(true);
  165. player.reset();
  166. player.setDataSource(file.getAbsolutePath());
  167. player.prepare();
  168. player.start();
  169. playBtn.setText(R.string.reset_text);
  170. run();
  171. }
  172. if (playBtn.getText().toString().equals("重置")) {
  173. pauseBtn.setEnabled(true);
  174. stopBtn.setEnabled(true);
  175. }
  176. if (pauseBtn.getText().toString().equals("继续")) {
  177. pauseBtn.setText(R.string.pause_text);
  178. }
  179. }
  180. }
  181. private void run() {
  182. new Thread(new Runnable() {
  183. public void run() {
  184. while (player != null) {
  185. int TIME = player.getDuration();
  186. seekBar.setMax(TIME);// 获取歌曲的最大值
  187. position = player.getCurrentPosition();// 返回当前播放进度值
  188. seekBar.setProgress(position);
  189. try {
  190. Thread.sleep(3000);
  191. } catch (InterruptedException e) {
  192. e.printStackTrace();
  193. }
  194. }
  195. }
  196. }).start();
  197. }
  198. public void onProgressChanged(SeekBar seekBar, int progress,
  199. boolean fromUser) {
  200. }
  201. public void onStartTrackingTouch(SeekBar seekBar) {
  202. player.seekTo(seekBar.getProgress());
  203. }
  204. public void onStopTrackingTouch(SeekBar seekBar) {
  205. player.seekTo(seekBar.getProgress());
  206. }
  207. }

package cn.csdn.playle; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; public class MusicPlayerActivity extends Activity implements OnClickListener, OnSeekBarChangeListener { EditText musicEt; Button playBtn, pauseBtn, stopBtn, chooseBtn; TextView time; AudioManager audiomanage; int maxVolume, currentVolume; SeekBar seekBar, seekBarSound; MediaPlayer player = null; File file = null; int position = 0; int i = 0; Handler handler = new Handler(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); player = new MediaPlayer(); findViews(); } private void findViews() { musicEt = (EditText) this.findViewById(R.id.musicEt); playBtn = (Button) this.findViewById(R.id.playBtn); pauseBtn = (Button) this.findViewById(R.id.pauseBtn); stopBtn = (Button) this.findViewById(R.id.stopBtn); chooseBtn = (Button) this.findViewById(R.id.chooseBtn); seekBar = (SeekBar) this.findViewById(R.id.seekBar); seekBarSound = (SeekBar) this.findViewById(R.id.seekBarSound); time = (TextView) this.findViewById(R.id.time); time.setOnClickListener(this); playBtn.setOnClickListener(this); pauseBtn.setOnClickListener(this); stopBtn.setOnClickListener(this); stopBtn.setEnabled(false); pauseBtn.setEnabled(false); seekBar.setOnSeekBarChangeListener(this); chooseBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MusicPlayerActivity.this, MusicListActivity.class); startActivity(intent); } }); audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE); maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量 seekBarSound.setMax(maxVolume); // 拖动条最高值与系统最大声匹配 currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值 seekBarSound.setProgress(currentVolume); seekBarSound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() // 调音监听器 { public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); currentVolume = audiomanage .getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值 seekBarSound.setProgress(currentVolume); } public void onStartTrackingTouch(SeekBar seekBar) { } public void onStopTrackingTouch(SeekBar seekBar) { } }); Intent intent = this.getIntent(); String name = intent.getStringExtra("name"); musicEt.setText(name); } public void onClick(View v) { String fileName = musicEt.getText().toString().trim(); // 获取sdcard的状态是否已加载 (MEDIA_MOUNTED 加载状态) if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { file = new File(Environment.getExternalStorageDirectory(), fileName); // 判断所播放的媒体文件是否存在 if (file.exists()) { try { switch (v.getId()) {// 返回一个int值 case R.id.playBtn: playMusic(file); break; case R.id.pauseBtn: if (player.isPlaying()) { player.pause(); pauseBtn.setText(R.string.continue_text); } else { player.start(); pauseBtn.setText(R.string.pause_text); } break; case R.id.stopBtn: player.stop(); stopBtn.setEnabled(false); seekBar.setProgress(0); if (playBtn.getText().toString().equals("重置")) { playBtn.setText(R.string.play_text); } if (pauseBtn.getText().toString().equals("继续")) { pauseBtn.setText(R.string.pause_text); pauseBtn.setEnabled(false); } else { pauseBtn.setEnabled(false); } break; } } catch (IllegalArgumentException e) { Log.e("TAG", e.toString()); } catch (IllegalStateException e) { Log.e("TAG", e.toString()); } catch (IOException e) { Log.e("TAG", e.toString()); } } else { Toast.makeText(this, R.string.notfoundfile_text, Toast.LENGTH_LONG).show(); } } else { Toast.makeText(this, R.string.notfoundSdcard_text, Toast.LENGTH_LONG).show(); } } protected void onDestroy() { if (player != null) { if (player.isPlaying()) { player.stop(); } player.release(); } super.onDestroy(); } protected void onPause() { if (player != null) { if (player.isPlaying()) { player.pause(); } } super.onPause(); } private void playMusic(File file) throws IllegalStateException, IOException { if (musicEt.getText().toString() == null || "".equals(musicEt.getText().toString())) { Toast.makeText(this, "没有选中歌曲", Toast.LENGTH_LONG).show(); } else { if (player != null) { pauseBtn.setEnabled(true); player.reset(); player.setDataSource(file.getAbsolutePath()); player.prepare(); player.start(); playBtn.setText(R.string.reset_text); run(); } if (playBtn.getText().toString().equals("重置")) { pauseBtn.setEnabled(true); stopBtn.setEnabled(true); } if (pauseBtn.getText().toString().equals("继续")) { pauseBtn.setText(R.string.pause_text); } } } private void run() { new Thread(new Runnable() { public void run() { while (player != null) { int TIME = player.getDuration(); seekBar.setMax(TIME);// 获取歌曲的最大值 position = player.getCurrentPosition();// 返回当前播放进度值 seekBar.setProgress(position); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } public void onStartTrackingTouch(SeekBar seekBar) { player.seekTo(seekBar.getProgress()); } public void onStopTrackingTouch(SeekBar seekBar) { player.seekTo(seekBar.getProgress()); } }

效果图:

image

你可能感兴趣的:(播放器)