Can someone give me an sample code for changing volume through a slider? I searched and a lot of tutorials requied me to create a whole new class. Is there an easier way?
Thanks!
Add this to your OnCreate, you have to put your seekbar into the layout xml file:
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
Travis at the New Boston has a great Video Tutorial on this here: http://www.youtube.com/watch?v=8sr2Y6Aff6Y
Source code for the tutorials can be found here: http://www.mybringback.com/bringers/android/thenewboston-android-series/828/thenewboston-sample-projects/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With