Android下调整多媒体音量方法

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {


AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);


   switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:// 音量增大
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume+1, 1);
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:// 音量减小
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume-1, 1);
break;


case KeyEvent.KEYCODE_BACK:// 返回键
//jniOnCallCppEvent();
    return true;
    //return super.onKeyDown(keyCode, event); 


default:
break;
}
   
   return true;
   //return super.onKeyDown(keyCode, event);
}

游戏过程中只允许调整多媒体音量,而不允许调整通话音量。

需要添加权限<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 

你可能感兴趣的:(android)