Android音量seekbar制作

很多音乐播放器界面上都有一个音量seekbar,那么在android里面是如何实现的呢?

   首先分析下要解决的问题:1.获取媒体播放的音量。

               2.通过seekbar可以增减音量

               3.用户按下音量键增减音量,seekbar保持同步

   对于第一个问题:Android系统提供AudioManager类来获得系统audio服务。

   对于第二个问题:实现seekBarChangeLIstener里面onProgressChanged方法。

    对于第三个问题:用线程来同步更新UI。

   设置音量的代码:

  
    
1 private void setVolum()
2 {
3
4 maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
5 volSeekBar.setMax(maxVolume);
6 currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
7 volSeekBar.setProgress(currentVolume);
8 mVolume.setText(currentVolume * 100 / maxVolume + " " );
9 }

    实现seekbar控制音量代码:

  
    
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

}

};

    线程更新UI代码:

  
    
1 Handler myHandler = new Handler(){
2 public void handleMessage(Message msg) {
3 switch (msg.what) {
4 case PROGRESS_CHANGED:
5 setVolum();
6 /* sendEmptyMessageDelayed(PROGRESS_CHANGED, 200); */
7 break ;
8 }
9 }
10 };
11
12   class myVolThread implements Runnable {
13 public void run() {
14 while ( ! Thread.currentThread().isInterrupted()) {
15
16 Message message = new Message();
17 message.what = PROGRESS_CHANGED;
18 MainActivity. this .myHandler.sendMessage(message);
19 try {
20 Thread.sleep( 100 );
21 } catch (InterruptedException e) {
22 Thread.currentThread().interrupt();
23 }
24 }
25 }
26 }

  最后贴上MainActivity代码:

01 public class MainActivity extends Activity {
02       
03     protected static final int PROGRESS_CHANGED = 0x101;
04     private SeekBar volSeekBar ;
05     private TextView mVolume ;
06     private AudioManager mAudioManager;
07     private int maxVolume, currentVolume;
08       
09     Thread myVolThread = null;
10     Handler myHandler = new Handler(){
11         public void handleMessage(Message msg) {  
12             switch (msg.what) {  
13             case PROGRESS_CHANGED:  
14                 setVolum();  
15                 /*sendEmptyMessageDelayed(PROGRESS_CHANGED, 200);*/
16                 break;
17            }
18         }
19     };
20     public void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.main);
23           
24         volSeekBar= (SeekBar)findViewById(R.id.volum_seek);
25         mVolume = (TextView)findViewById(R.id.volum_val);
26         mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
27           
28         //setVolum();
29         OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener()
30         {
31   
32             public void onProgressChanged(SeekBar seekBar, int progress,
33                     boolean fromUser) {
34                 // TODO Auto-generated method stub
35                 mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
36             }
37   
38             public void onStartTrackingTouch(SeekBar seekBar) {
39                 // TODO Auto-generated method stub
40                   
41             }
42   
43             public void onStopTrackingTouch(SeekBar seekBar) {
44                 // TODO Auto-generated method stub
45                   
46             }
47               
48         };
49           
50         volSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
51         new Thread(new myVolThread()).start();
52           
53     }
54     private void setVolum()
55     {
56           
57         maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
58         volSeekBar.setMax(maxVolume);
59         currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
60         volSeekBar.setProgress(currentVolume);
61         mVolume.setText(currentVolume*100/maxVolume + " ");
62     }
63       
64     class myVolThread implements Runnable {   
65         public void run() {  
66              while (!Thread.currentThread().isInterrupted()) {    
67                        
68                   Message message = new Message();   
69                   message.what =PROGRESS_CHANGED;   
70                   MainActivity.this.myHandler.sendMessage(message);   
71                   try {   
72                        Thread.sleep(100);    
73                   } catch (InterruptedException e) {   
74                        Thread.currentThread().interrupt();   
75                   }   
76              }   
77         }   
78    }   
79 }

你可能感兴趣的:(android)