Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control media volume?

Tags:

android

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?

like image 747
Badr Hari Avatar asked Jul 22 '11 01:07

Badr Hari


People also ask

What is media volume on my phone?

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.


2 Answers

In your activity's onCreate() you can do:

setVolumeControlStream(AudioManager.STREAM_MUSIC); 
like image 160
Kevin TeslaCoil Avatar answered Sep 27 '22 22:09

Kevin TeslaCoil


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.

like image 44
Squonk Avatar answered Sep 28 '22 00:09

Squonk