Android sliding scroll bar to adjust the volume, android scroll bar
Use scroll bar sliding to control the volume:
Definition:
1 private SeekBar mseekBarvolume;
The following code is used:
1 // adjust the volume -- begin ------------------------- 2 // volume control, initialization Definition 3 mAudioManager = (AudioManager) getSystemService (Context. AUDIO_SERVICE); 4 // obtain the maximum volume of 5 int maxVolume = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_MUSIC); 6 // get the current volume 7 syscurrenvolume = mAudioManager. getStreamVolume (AudioManager. STREAM_MUSIC); 8 9 mseekBarvolume = (SeekBar) findViewById (R. id. seekBar_volume); 10 // The maximum volume bound to the progress bar. The maximum volume is 11 mse. EkBarvolume. setMax (maxVolume); 12 // The progress bar is bound to the current volume of 13 mseekBarvolume. setProgress (syscurrenvolume); 14 15 // adjust the volume event 16 mseekBarvolume. setOnSeekBarChangeListener (new OnSeekBarChangeListener () {17 18 @ Override 19 public void onStopTrackingTouch (SeekBar seekBar) {20 // release event 21} 22 23 @ Override 24 public void onStartTrackingTouch (SeekBar seekBar) {25 // TODO Auto-generated method stub 26} 27 28 @ Overrid E 29 public void onProgressChanged (SeekBar seekBar, int progress, 30 boolean fromUser) {31 // TODO Auto-generated method stub 32 // get the current progress 33 int tmpInt = seekBar. getProgress (); 34 35 // when the progress is less than 1, set it to 1 to avoid being too small. 36 if (tmpInt <1) {37 tmpInt = 1; 38} 39 40 // adjust the brightness of 41 mAudioManager according to the current progress. setStreamVolume (AudioManager. STREAM_MUSIC, tmpInt, 0); 42} 43}); 44 // adjust the volume -- end -------------- 45 47} 48
Refer to the following code for implementation:
Android volume control adjustment (