开发者

Enable Media Volume Slider in Android application

开发者 https://www.devze.com 2022-12-28 13:28 出处:网络
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.

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 开发者_StackOverflow社区volume slider?

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


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


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消