简单音乐播放器
实现的功能
- 实现播放,暂停,播放上一首,下一首功能
- 显示播放列表
- 可以播放多首歌
实现情况
截图
主要功能核心代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package com.example.mp3demo; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Mp3Demo extends Activity {
private Button start= null ; private Button pause= null ; private Button stop= null ; private TextView state= null ; private MediaPlayer mp3; private Boolean flag= false ; //用false表示正在播放 @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); super .setContentView(R.layout.activity_mp3_demo); start=(Button) super .findViewById(R.id.start); pause=(Button) super .findViewById(R.id.pause); stop=(Button) super .findViewById(R.id.stop); state=(TextView) super .findViewById(R.id.state); start.setOnClickListener( new OnClickListenerStart()); pause.setOnClickListener( new OnClickListenerPause()); stop.setOnClickListener( new OnClickListenerStop()); mp3= new MediaPlayer();
mp3 = MediaPlayer.create(Mp3Demo. this ,R.raw.sky); } //开始播放 private class OnClickListenerStart implements OnClickListener{ public void onClick(View v) { try { if (mp3!= null ) { mp3.stop(); } mp3.prepare(); mp3.start(); //开始播放 state.setText( "Playing" );
} catch (Exception e) { state.setText(e.toString()); e.printStackTrace(); } } } //暂停播放 private class OnClickListenerPause implements OnClickListener{ @Override public void onClick(View v) { try { if (flag== false ) { mp3.pause(); flag= true ; state.setText( "pause" ); } else if (flag== true ){ mp3.start(); //开始播放 flag= false ; state.setText( "Playing" ); } } catch (Exception e) { state.setText(e.toString()); e.printStackTrace(); } } } //停止播放 private class OnClickListenerStop implements OnClickListener{ @Override public void onClick(View v) { try { if (mp3!= null ) { mp3.stop(); state.setText( "stop" ); } } catch (Exception e) { state.setText(e.toString()); e.printStackTrace(); } } } protected void onPause(){ try { mp3.release(); //释放音乐资源 } catch (Exception e) { state.setText(e.toString()); e.printStackTrace(); } super .onPause(); } } |
布局文件核心代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > android:id= "@+id/state" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world" /> android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "horizontal" > android:id= "@+id/start" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "start" /> android:id= "@+id/pause" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "pause" /> android:id= "@+id/stop" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "stop" /> |
代码地址:https://coding.net/u/insist_shen/p/musicplay/git