Hello i am new to android. I have 2 Imagebuttons. 1 for increasing volume and another for decreasing. And i have one Seekbar to show increase and decrease levels. I want an android code to increase as well as decrease system volumes and it should also reflect the Seekbar. Can anyone please help me
0
votes
1 Answers
0
votes
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
Button increaseVolume = (Button) findViewById(R.id.button2);
Button decreaseVolume = (Button) findViewById(R.id.button3);
seekBar.setMax(audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audio.getStreamVolume(AudioManager.STREAM_MUSIC));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
audio.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_SHOW_UI);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
increaseVolume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (seekBar.getProgress() < audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {
seekBar.setProgress(seekBar.getProgress() + 1);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, seekBar.getProgress(), AudioManager.FLAG_SHOW_UI);
}
}
});
decreaseVolume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (seekBar.getProgress() > 0) {
seekBar.setProgress(seekBar.getProgress() - 1);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, seekBar.getProgress(), AudioManager.FLAG_SHOW_UI);
}
}
});