Volume keys

In my application, I redefined the onKeyDown() and onKeyUp() functions to capture volume key events. I use such events to control the volume of my application. I use a music stream to play my sounds. Upon detecting such an event, I also show a custom toast (similar to the one shown by Android). I came across this:

  • Android always plays sound on volume keys
  • This sound is always reproduced with the same intensity.

I would like to control the default audio playback intensity (also the stream on which it is playing) as follows: a louder sound for a higher volume and a lower sound for a lower volume, if possible. Or a way to turn off the default playback of this sound and play your own sound in intensity that I just set.

+4
source share
2 answers

Actually, the sound is played on onKeyUp (...), so you can simply overload the method in its activity when it is 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); } 

It worked for me :)

+9
source

The strange reason I wrote this kind of functionality, and Android seems to play louder when you raise the volume of the stream.

 am.setStreamVolume(AudioManager.STREAM_MUSIC, progress,AudioManager.FLAG_PLAY_SOUND); 

Here is what I used in my application. am is an instance of AudioManager that you can get by writing:

 AudioManager am = (AlarmManager) getSystemService(AUDIO_SERVICE); 

To turn off the sound, you can replace AudioManager.FLAG_PLAY_SOUND with the value "0", which should turn it off.

I'm not sure you can replace this sound in AudioManager, but you can play these custom sounds using MediaPlayer inside your onKeyDown methods.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1332905/


All Articles