android 调节媒体音量

引用 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);
}

  

posted on 2011-11-02 10:09  John Smith  阅读(3292)  评论(0编辑  收藏  举报