首页 > 代码库 > Android中如何禁止音量调节至静音

Android中如何禁止音量调节至静音

Android中音量按键在调低音量时,如果一直按住Down按钮不放,则系统将音量跳到最小后,又自动调节到静音状态。这个机制和iPhone是不同的,iPhone中无论你怎么按Volume-按钮,只能调到最小,还是有一点声音,是不能自动静音的。

Android中如果要禁止这个自动静音功能,也是有办法的,找到Down按钮的处理事件,做个判断就行了,代码在:

frameworks/base/media/java/android/media/AudioManager.java

public void handleKeyDown(KeyEvent event, int stream) {        int keyCode = event.getKeyCode();        switch (keyCode) {            case KeyEvent.KEYCODE_VOLUME_UP:            case KeyEvent.KEYCODE_VOLUME_DOWN:                /*                 * Adjust the volume in on key down since it is more                 * responsive to the user.                 */                int flags = FLAG_SHOW_UI | FLAG_VIBRATE;                /* avoid auto mute when adjust lower */                int currentVolume = getStreamVolume(STREAM_MUSIC);                Log.e(TAG, String.format("currentVolume=%d", currentVolume));                if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && currentVolume <= 1) {                    break;                }                if (mUseMasterVolume) {                    adjustMasterVolume(                            keyCode == KeyEvent.KEYCODE_VOLUME_UP                                    ? ADJUST_RAISE                                    : ADJUST_LOWER,                            flags);                } else {

 

Android中如何禁止音量调节至静音