android 音量获取和设置总结

1、获取音量值:

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

其中AudioManager.STREAM_MUSIC代表媒体音量,也可以替换成其他的类型,获取其他类型音量。


2、设置音量值:

例如我把媒体音量设置为0(静音)

    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);


3、监听音量键被按下:


在activity重写onKeyDown方法

public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(TAG, "onKeyDown" + keyCode + "" + (keyCode == KeyEvent.KEYCODE_VOLUME_UP));
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {

    // 音量+键

   }

   if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){

   // 音量-键

    }

}

4、监听音量变化:

上面通过监听按键来监听调整音量的方法,不是很靠谱,因为可能用户在设置里调整音量,所以用下面方式监听音量变化


private void registerVolumeChangeReceiver() {
    mSettingsContentObserver = new SettingsContentObserver(mContext, new Handler());
    mContext.getContentResolver()
            .registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
}

private void unregisterVolumeChangeReceiver() {
    mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
}

public class SettingsContentObserver extends ContentObserver {
    Context context;

    public SettingsContentObserver(Context c, Handler handler) {
        super(handler);
        context = c;
    }

    @Override
    public boolean deliverSelfNotifications() {
        return super.deliverSelfNotifications();
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.d(TAG, "音量:" + currentVolume);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
    }
}


5、监听震动模式变化


public boolean isMuteMode() {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
}

public boolean isVibrateMode() {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;
}

public boolean isNormalMode() {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
}

private void registerRingerModeReceiver() {
    mRingerModeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
                // 监听到震动/静音/响铃的模式变化

                  if( isMuteMode()){

                 }
            }
        }
    };
    IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
    mContext.registerReceiver(mRingerModeReceiver, filter);
}



你可能感兴趣的:(Android)