I solved a similar problem. I had to limit the maximum volume in the application, so this is what I did (audio is an instance of AudioManager):
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP){
if(audio != null){
int curVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = Preferences.getDefaultMaxVolume(Main.this);
if(curVolume > maxVolume) audio.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
}
}
return super.onKeyDown(keyCode, event);
}
You can simply use the setStreamVolume () method to change progrmatically volumes if necessary ...
source
share