I would like to control media volume with my app using regular volume buttons. The problem is my app is using short sounds, and they might be annoying for the user. You can only adjust media volume while they are playing (for 1 sec) then it starts to adjust ringer volume. How can I make media volume default?
Under "Media volume," tap Play media to. When you press a volume button, the volume that changes depends on what you're doing. For example, if you're watching a movie, the movie volume changes. If you press your volume buttons when you're not listening to anything, your media volume changes.
In your activity's onCreate() you can do:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
I haven't tried this but you should be able to intercept the regular volume buttons by overriding onKeyDown(int keyCode, KeyEvent event)
in your Activity. Check for keycodes KEYCODE_VOLUME_UP
and KEYCODE_VOLUME_DOWN
.
Using AudioManager.setStreamVolume(int, int, int) with STREAM_MUSIC
for the streamType
parameter should work and if you return true
from the onKeyDown(...)
method (to indicate you've handled the event), it should prevent the system from adjusting the ringer volume. Make sure you return false
for all other keycodes that you're not handling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With