1 package com.hanqi.musicplayer;
2
3 import android.media.MediaPlayer;
4 import android.os.Bundle;
5 import android.os.Handler;
6 import android.os.Message;
7 import android.support.v7.app.AppCompatActivity;
8 import android.view.View;
9 import android.widget.ImageView;
10 import android.widget.ProgressBar;
11 import android.widget.TextView;
12
13 public class MainActivity extends AppCompatActivity {
14
15 ProgressBar pb_1;
16 TextView tv_1;
17 //SeekBar se_1;
18 ImageView iv_1;
19
20 MediaPlayer mediaPlayer;
21
22 Handler handler;
23
24 @Override
25 protected void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.activity_main);
28
29 pb_1=(ProgressBar)findViewById(R.id.pb_1);
30 tv_1=(TextView)findViewById(R.id.tv_1);
31 iv_1=(ImageView)findViewById(R.id.iv_1);
32 //se_1=(SeekBar)findViewById(R.id.se_1);
33 final int [] iv={R.drawable.f1,
34 R.drawable.f2,
35 R.drawable.f3,
36 R.drawable.f4,
37 R.drawable.f5,
38 R.drawable.f6,
39 R.drawable.f7,
40 R.drawable.f8,
41 R.drawable.f9,
42 R.drawable.f10};
43
44 //消息
45 handler = new Handler(){
46 @Override
47 public void handleMessage(Message msg) {
48 super.handleMessage(msg);
49
50 iv_1.setImageResource(iv[msg.arg1]);
51 }
52 };
53
54 //图片轮播 在分线程
55
56 new Thread(new Runnable() {
57 @Override
58 public void run() {
59
60
61 for (int i = 0; i < iv.length; i++)
62 {
63
64 //iv_1.setImageResource(R.drawable.f1);
65
66 Message message = new Message();
67
68 message.what=0;
69 message.arg1=i;
70 handler.sendMessage(message);
71
72 try {
73 Thread.sleep(3000);
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77
78 if (i == iv.length - 1) i = -1;
79 }
80 }
81
82 }).start();
83
84
85
86 }
87
88 //播放
89 public void play_OnClick(View v)
90 {
91 //构造mediaPlayer
92 if (mediaPlayer == null)
93 {
94 //用静态方法构造
95 mediaPlayer = MediaPlayer.create(this,R.raw.test);
96
97 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
98 @Override
99 public void onCompletion(MediaPlayer mp) {
100
101 tv_1.setText("播放状态:播放完成");
102
103 mediaPlayer.reset();//重置
104 mediaPlayer.release();//释放
105
106 mediaPlayer = null;
107 }
108 });
109
110 //设置进度条的最大进度
111 pb_1.setMax(mediaPlayer.getDuration());
112
113 //启动分线程
114 new Thread(){
115 @Override
116 public void run() {
117
118 //循环条件
119 while (mediaPlayer!=null)
120 {
121 //更新进度
122 // 设置当前进度:和播放器的当前进度有关
123 pb_1.setProgress(mediaPlayer.getCurrentPosition());
124
125 try
126 {
127 Thread.sleep(500);
128 }
129 catch (Exception e)
130 {
131 e.printStackTrace();
132 }
133 }
134 }
135 }.start();
136
137 }
138
139
140 // new Thread(new Runnable() {
141 // @Override
142 // public void run() {
143 // while (true) {
144 //
145 // pb_1.setProgress(mediaPlayer.getCurrentPosition());
146 //
147 // try
148 // {
149 // Thread.sleep(1000);
150 // }
151 // catch (Exception e)
152 // {
153 // e.printStackTrace();
154 // }
155 //
156 //
157 // }
158 // }
159 // })
160 // .start();
161
162 mediaPlayer.start();
163
164 tv_1.setText("播放状态:正在播放");
165 }
166
167 //停止
168 public void stop_OnClick(View v)
169 {
170 if (mediaPlayer!=null)
171 {
172 mediaPlayer.stop();
173 mediaPlayer.reset();
174 mediaPlayer.release();
175 mediaPlayer = null;
176
177 pb_1.setProgress(0);
178
179 tv_1.setText("播放状态:停止播放");
180 }
181 }
182
183 //暂停
184 public void pause_OnClick(View v)
185 {
186 if (mediaPlayer !=null&&mediaPlayer.isPlaying())
187 {
188 mediaPlayer.pause();
189
190 tv_1.setText("播放状态:暂停播放");
191 }
192 }
193
194 //退出
195 public void exit_OnClick(View v)
196 {
197 stop_OnClick(v);
198
199 finish();
200 }
201 }