---
title: " Android成长之路-音乐播放器的实现(转)"
author: "云端的Android"
date: "2012-01-03T00:23:40Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-01-03T00:28:18Z"
id: 1333340
url: "https://lvlayster.iteye.com/blog/1333340"
---

strings.xml

[view plain](http://blog.csdn.net/like7xiaoben/article/details/7171783 "view plain")

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;resources&gt;
03. &lt;string name="app\_name"&gt;MusicPlayer&lt;/string&gt;
04. &lt;string name="music\_name"&gt;曲目&lt;/string&gt;
05. &lt;string name="play\_text"&gt;播放&lt;/string&gt;
06. &lt;string name="pause\_text"&gt;暂停&lt;/string&gt;
07. &lt;string name="continue\_text"&gt;继续&lt;/string&gt;
08. &lt;string name="reset\_text"&gt;重置&lt;/string&gt;
09. &lt;string name="stop\_text"&gt;停止&lt;/string&gt;
10. &lt;string name="choose\_text"&gt;选择&lt;/string&gt;
11. &lt;string name="notfoundfile\_text"&gt;媒体文件不存在&lt;/string&gt;
12. &lt;string name="notfoundSdcard\_text"&gt;SDcard不存在&lt;/string&gt;
13. &lt;/resources&gt;

 

 

 

main.xml

[view plain](http://blog.csdn.net/like7xiaoben/article/details/7171783 "view plain")

01. &lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout\_width="fill\_parent"
04. android:layout\_height="fill\_parent"
05. android:orientation="vertical" &gt;
06. &lt;TextView
07. android:layout\_width="fill\_parent"
08. android:layout\_height="wrap\_content"
09. android:text="@string/music\_name" /&gt;
10. &lt;TableLayout
11. android:layout\_width="fill\_parent"
12. android:layout\_height="wrap\_content"
13. android:stretchColumns="0" &gt;
14. &lt;TableRow &gt;
15. &lt;EditText
16. android:id="@+id/musicEt"
17. android:layout\_width="fill\_parent"
18. android:layout\_height="wrap\_content"
19. android:text="l.mp3" /&gt;
20. &lt;Button
21. android:id="@+id/chooseBtn"
22. android:layout\_width="wrap\_content"
23. android:layout\_height="wrap\_content"
24. android:text="@string/choose\_text" /&gt;
25. &lt;/TableRow&gt;
26. &lt;/TableLayout&gt;
27. &lt;SeekBar
28. android:id="@+id/seekBar"
29. android:layout\_width="fill\_parent"
30. android:layout\_height="wrap\_content" /&gt;
31. &lt;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"/&gt;
37. /&gt;
38. &lt;TextView
39. android:id="@+id/time"
40. android:layout\_width="fill\_parent"
41. android:layout\_height="wrap\_content"
42. /&gt;
43. &lt;TableLayout
44. android:layout\_width="fill\_parent"
45. android:layout\_height="wrap\_content"
46. android:stretchColumns="\*" &gt;
47. &lt;TableRow &gt;
48. &lt;Button
49. android:id="@+id/playBtn"
50. android:layout\_width="wrap\_content"
51. android:layout\_height="wrap\_content"
52. android:text="@string/play\_text" /&gt;
53. &lt;Button
54. android:id="@+id/pauseBtn"
55. android:layout\_width="wrap\_content"
56. android:layout\_height="wrap\_content"
57. android:text="@string/pause\_text" /&gt;
58. &lt;Button
59. android:id="@+id/stopBtn"
60. android:layout\_width="wrap\_content"
61. android:layout\_height="wrap\_content"
62. android:text="@string/stop\_text" /&gt;
63. &lt;/TableRow&gt;
64. &lt;/TableLayout&gt;
65. &lt;/LinearLayout&gt;

 

MusicPlayerActivity.java

[view plain](http://blog.csdn.net/like7xiaoben/article/details/7171783 "view plain")

001. package cn.csdn.playle;
002. import java.io.File;
003. import java.io.IOException;
004. import android.app.Activity;
005. import android.content.Context;
006. import android.content.Intent;
007. import android.media.AudioManager;
008. import android.media.MediaPlayer;
009. import android.os.Bundle;
010. import android.os.Environment;
011. import android.os.Handler;
012. import android.util.Log;
013. import android.view.View;
014. import android.view.View.OnClickListener;
015. import android.widget.Button;
016. import android.widget.EditText;
017. import android.widget.SeekBar;
018. import android.widget.SeekBar.OnSeekBarChangeListener;
019. import android.widget.TextView;
020. import android.widget.Toast;
021. public class MusicPlayerActivity extends Activity implements OnClickListener,
022. OnSeekBarChangeListener {
023. EditText musicEt;
024. Button playBtn, pauseBtn, stopBtn, chooseBtn;
025. TextView time;
026. AudioManager audiomanage;
027. int maxVolume, currentVolume;
028. SeekBar seekBar, seekBarSound;
029. MediaPlayer player = null;
030. File file = null;
031. int position = 0;
032. int i = 0;
033. Handler handler = new Handler();
034. public void onCreate(Bundle savedInstanceState) {
035. super.onCreate(savedInstanceState);
036. setContentView(R.layout.main);
037. player = new MediaPlayer();
038. findViews();
039. }
040. private void findViews() {
041. musicEt = (EditText) this.findViewById(R.id.musicEt);
042. playBtn = (Button) this.findViewById(R.id.playBtn);
043. pauseBtn = (Button) this.findViewById(R.id.pauseBtn);
044. stopBtn = (Button) this.findViewById(R.id.stopBtn);
045. chooseBtn = (Button) this.findViewById(R.id.chooseBtn);
046. seekBar = (SeekBar) this.findViewById(R.id.seekBar);
047. seekBarSound = (SeekBar) this.findViewById(R.id.seekBarSound);
048. time = (TextView) this.findViewById(R.id.time);
049. time.setOnClickListener(this);
050. playBtn.setOnClickListener(this);
051. pauseBtn.setOnClickListener(this);
052. stopBtn.setOnClickListener(this);
053. stopBtn.setEnabled(false);
054. pauseBtn.setEnabled(false);
055. seekBar.setOnSeekBarChangeListener(this);
056. chooseBtn.setOnClickListener(new OnClickListener() {
057. public void onClick(View v) {
058. Intent intent = new Intent(MusicPlayerActivity.this,
059. MusicListActivity.class);
060. startActivity(intent);
061. }
062. });
063. audiomanage = (AudioManager) getSystemService(Context.AUDIO\_SERVICE);
064. maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM\_MUSIC); // 获取系统最大音量
065. seekBarSound.setMax(maxVolume); // 拖动条最高值与系统最大声匹配
066. currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM\_MUSIC); // 获取当前值
067. seekBarSound.setProgress(currentVolume);
068. seekBarSound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() // 调音监听器
069. {
070. public void onProgressChanged(SeekBar arg0, int progress,
071. boolean fromUser) {
072. audiomanage.setStreamVolume(AudioManager.STREAM\_MUSIC,
073. progress, 0);
074. currentVolume = audiomanage
075. .getStreamVolume(AudioManager.STREAM\_MUSIC); // 获取当前值
076. seekBarSound.setProgress(currentVolume);
077. }
078. public void onStartTrackingTouch(SeekBar seekBar) {
079. }
080. public void onStopTrackingTouch(SeekBar seekBar) {
081. }
082. });
083. Intent intent = this.getIntent();
084. String name = intent.getStringExtra("name");
085. musicEt.setText(name);
086. }
087. public void onClick(View v) {
088. String fileName = musicEt.getText().toString().trim();
089. // 获取sdcard的状态是否已加载 (MEDIA\_MOUNTED 加载状态)
090. if (Environment.getExternalStorageState().equals(
091. Environment.MEDIA\_MOUNTED)) {
092. file = new File(Environment.getExternalStorageDirectory(), fileName);
093. // 判断所播放的媒体文件是否存在
094. if (file.exists()) {
095. try {
096. switch (v.getId()) {// 返回一个int值
097. case R.id.playBtn:
098. playMusic(file);
099. 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. 效果图：
     
     ![](http://hi.csdn.net/attachment/201201/2/0_1325503975zPwJ.gif)