How do you get/set media volume (not ringtone volu

2019-01-08 09:35发布

Is there a way to get/set media volume? I have tried the following:

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);

but it returns the ringtone volume.

8条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-08 10:36

Have a try with this:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

查看更多
放荡不羁爱自由
3楼-- · 2019-01-08 10:41
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;
    }
}
查看更多
登录 后发表回答