ITeye博客

Android成长之路-音乐播放器的实现(转)

云端的Android · 2012-01-03 00:23 · Android点滴 · 移动开发 · Markdown

strings.xml

view plain

  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>

 

 

 

main.xml

view plain

  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>

 

MusicPlayerActivity.java

view plain

  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. }

  208. 效果图:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。