How can I manage audio volumes sanely in my Android app?

Got another suggestion via Google Groups that is the platform integrated solution I was looking for and works fine:

Please don't handle the volume keys yourself - it is almost impossible to guarantee that you won't break the behavior of the volume keys.

Call this API in your onCreate():

setVolumeControlStream(AudioManager.STREAM_MUSIC);

This tells the AudioManager that when your application has focus, the volume keys should adjust music volume.


If you are using the 'OnKeyDown' method, you can get around this issue by doing the following:

public boolean onKeyDown(int keyCode, KeyEvent event) {    
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                setVolumeControlStream(AudioManager.STREAM_MUSIC);}}

It is usefull of this API: setVolumeControlStream(AudioManager.STREAM_MUSIC);

Unless this I will spend more time to fix that volume bug.

Tags:

Android