下文来自:
http://www.itivy.com/android/archive/2011/7/10/android-set-voice-seekbar.html
--------------------------------------------
首先分析下要解决的问题:
1.获取媒体播放的音量。
2.通过seekbar可以增减音量
3.用户按下音量键增减音量,seekbar保持同步
对于第一个问题:Android系统提供AudioManager类来获得系统audio服务。
对于第二个问题:实现seekBarChangeLIstener里面onProgressChanged方法。
对于第三个问题:用线程来同步更新UI。
设置音量的代码:
1
2
3
4
5
6
7
8
|
private
void
setVolum()
{
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volSeekBar.setMax(maxVolume);
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volSeekBar.setProgress(currentVolume);
mVolume.setText(currentVolume*
100
/maxVolume +
" "
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
OnSeekBarChangeListener seekBarChangeListener =
new
OnSeekBarChangeListener()
{
public
void
onProgressChanged(SeekBar seekBar,
int
progress,
boolean
fromUser) {
// TODO Auto-generated method stub
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
0
);
}
public
void
onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public
void
onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
|
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
|
Handler myHandler =
new
Handler(){
public
void
handleMessage(Message msg) {
switch
(msg.what) {
case
PROGRESS_CHANGED:
setVolum();
/*sendEmptyMessageDelayed(PROGRESS_CHANGED, 200);*/
break
;
}
}
};
class
myVolThread
implements
Runnable {
public
void
run() {
while
(!Thread.currentThread().isInterrupted()) {
Message message =
new
Message();
message.what =PROGRESS_CHANGED;
MainActivity.
this
.myHandler.sendMessage(message);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
|
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
|
public
class
MainActivity
extends
Activity {
protected
static
final
int
PROGRESS_CHANGED =
0x101
;
private
SeekBar volSeekBar ;
private
TextView mVolume ;
private
AudioManager mAudioManager;
private
int
maxVolume, currentVolume;
Thread myVolThread =
null
;
Handler myHandler =
new
Handler(){
public
void
handleMessage(Message msg) {
switch
(msg.what) {
case
PROGRESS_CHANGED:
setVolum();
/*sendEmptyMessageDelayed(PROGRESS_CHANGED, 200);*/
break
;
}
}
};
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
volSeekBar= (SeekBar)findViewById(R.id.volum_seek);
mVolume = (TextView)findViewById(R.id.volum_val);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//setVolum();
OnSeekBarChangeListener seekBarChangeListener =
new
OnSeekBarChangeListener()
{
public
void
onProgressChanged(SeekBar seekBar,
int
progress,
boolean
fromUser) {
// TODO Auto-generated method stub
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
0
);
}
public
void
onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public
void
onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
volSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
new
Thread(
new
myVolThread()).start();
}
private
void
setVolum()
{
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volSeekBar.setMax(maxVolume);
currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volSeekBar.setProgress(currentVolume);
mVolume.setText(currentVolume*
100
/maxVolume +
" "
);
}
class
myVolThread
implements
Runnable {
public
void
run() {
while
(!Thread.currentThread().isInterrupted()) {
Message message =
new
Message();
message.what =PROGRESS_CHANGED;
MainActivity.
this
.myHandler.sendMessage(message);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
|
-----------------------------------------
注:原来我为了保持与 系统按键操作的统一,在Acitity中通过
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_VOLUME_DOWN:
ddApplication.reSaveMusicVolume();
break;
case KeyEvent.KEYCODE_VOLUME_UP:
ddApplication.reSaveMusicVolume();
break;
}
musicControl.setProgress(ddApplication.getMusicVolume()); // 这一句不起作用啊
return super.onKeyDown(keyCode, event);
}
结果发现并不能实现同步的效果,遂找得上篇来作参考!