android 调节媒体音量

原文链接:http://www.cnblogs.com/tracydj/archive/2011/11/02/2232640.html

引用 AudioManager 对象

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

重写 Activity 的 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_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_LOWER,
            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        break;
    }
    return super.onKeyDown(keyCode, event);
}

  

转载于:https://www.cnblogs.com/tracydj/archive/2011/11/02/2232640.html

上一篇:Android P 音频焦点管理


下一篇:Android音频焦点申请处理