Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control volume keys

In my application I have overridden the onKeyDown() and onKeyUp() functions in order to capture volume keys events. I use such events in order to control my application volume. I use the music stream to play my sounds. When detecting an event like this I also show a custom toast (similar to the one shown by Android). The problems I'm facing with this take are:

  1. Android always plays a sound on volume keys events
  2. That sound is played always at the same intensity.

What I'd like is to control the intensity at which the default sound is played (also the stream on which is played) in the following way: a louder sound for a higher volume and a lower sound for a low volume, if this is possible. Or a way to disable playing that default sound and play my custom sound at the intensity I just set.

like image 588
rantravee Avatar asked Jan 20 '23 21:01

rantravee


1 Answers

Actually the sound is played on onKeyUp(...), so you can simply overload the method in your activity when it gets called for the volume keys :

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
       return true;
    }
    return super.onKeyUp(keyCode, event);
}

This worked for me :)

like image 62
Gregory Avatar answered Jan 23 '23 10:01

Gregory