Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Media Volume Slider in Android application

Tags:

android

Some Android programs trigger the "media volume" slider when the hardware volume up/down buttons are pressed. My application seems to set the ringer volume when the hardware buttons are pressed.

How would I enable the media volume slider?

I would hate for users to have to go into their settings to change the media volume when they use my application.

like image 887
Oh Danny Boy Avatar asked Apr 30 '10 13:04

Oh Danny Boy


People also ask

How do I get volume widget?

Just grab a copy of Slider Widget - Volume & more for your Android device and you're ready to start customizing. Press and hold on one of your Android Home screens and then choose the size widget you want to get started with. After the widget appears, just tap on one of the icons for the sliding bar to pop up.

How do I add volume icon to Android?

The app also has a widget that does allow you to adjust the volume directly from the widget. To find the widget long-press on an empty space on your home screen and swipe until you see the Volume Control options. Choose the large widget and drag it to where you want to place it.


2 Answers

Just call Activity.setVolumeControlStream(AudioManager.STREAM_MUSIC) in your Activity's onCreate method.

like image 164
Christopher Orr Avatar answered Oct 07 '22 02:10

Christopher Orr


private AudioManager audio;

Inside onCreate:

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

Override onKeyDown:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        return false;
    }
}
like image 22
Anthony Graglia Avatar answered Oct 07 '22 02:10

Anthony Graglia