0

In my application I override volume up and volume down keys. The problem is that when user click one of these two keys, sound is played. I want somehow to disable/mute this sound.

Here is fragment of my code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {

        case KeyEvent.KEYCODE_VOLUME_DOWN: {
            // do something when user click volume down key

            return true;
        }
        case KeyEvent.KEYCODE_VOLUME_UP: {
            // do something when user click volume up key

            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}
Da2da2
  • 57
  • 2
  • 3
  • 13

2 Answers2

1

Using onKeyDown or onKeyUp still gives the default sound of changing the volume. Use dispatchKeyEvent instead.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}
  • Using `dispatchKeyEvent` you can detect whenever a button is pressed but you can't detect in which position is the button (up or down). In my example I used `onKeyDown`. This method doesn't remove the sound, so I had to play an infinite sound. – Da2da2 Feb 28 '18 at 17:16
  • No, in my example you can see keyCode and use it in the same way. It works, I use this method in my app. – Alexander Ivanov Feb 28 '18 at 18:48
  • I am not saying that your method doesn't work, it is alright but not in my situation. I want as I said to detect when the button is in position **up** or **down**. If I press a button (for example `KEYCODE_VOLUME_UP` ) once, `dispatchKeyEvent` will be called two times, if I do it with `onKeyDown` and `onKeyUp` , they will fire one after the other once, leaving me with more detailed information of the button current state. – Da2da2 Feb 28 '18 at 20:29
  • 1
    But you can check in dispatchKeyEvent: `if (event.getAction() == KeyEvent.ACTION_DOWN) ...` – Alexander Ivanov Mar 01 '18 at 04:22
0

I figured it out for myself. There is a way to stop the sound when volume key is pressed. You need to play an empty sound infinitely, so when user click a volume key it is "trying" to change the media volume, so there isn't click sound played. You can download an empty sound by searching for empty sound and then put it in your raw folder in the project. The last step is just to override the volume keys.

Here is the code you need:

Define MediaPlayer variable.

private MediaPlayer infinite_sound;

In onResume() you need to play the empty sound infinitely.

@Override
protected void onResume() {
    super.onResume();

    infinite_sound = MediaPlayer.create(this, R.raw.empty);
    infinite_sound.setLooping(true);
    infinite_sound.start();
}

In onPause() you need to stop the sound.

@Override
public void onPause() {
    super.onPause();

    infinite_sound.release();
}

And finally overriding the actions of the buttons.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN: {
            // Do something when volume down button is clicked

            return true;
        }
        case KeyEvent.KEYCODE_VOLUME_UP: {
            // Do something when volume up button is clicked

            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}
Da2da2
  • 57
  • 2
  • 3
  • 13