1、关闭/开启单一音量控制
frameworks/base/core/res/res/values/config.xml
<!-- Flag indicating whether all audio streams should be mapped toone single stream. If true, all audio streams are mapped toSTREAM_MUSIC as if it's on TV platform. --><bool name="config_single_volume">false</bool>
开启单一音量控制后,系统音量控制将直接跟随媒体音量,由其单一控制;
2、单一音量控制流程解析
frameworks/base/media/java/android/media/AudioSystem.java
/*** @hide* @return whether the system uses a single volume stream.*/public static boolean isSingleVolume(Context context) {boolean forceSingleVolume = context.getResources().getBoolean(com.android.internal.R.bool.config_single_volume);return getPlatformType(context) == PLATFORM_TELEVISION || forceSingleVolume;}
frameworks/base/services/core/java/com/android/server/audio/AudioService.java
mIsSingleVolume = AudioSystem.isSingleVolume(context);...private void updateStreamVolumeAlias(boolean updateVolumes, String caller) {int dtmfStreamAlias;final int a11yStreamAlias = sIndependentA11yVolume ?AudioSystem.STREAM_ACCESSIBILITY : AudioSystem.STREAM_MUSIC;if (mIsSingleVolume) {mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION;dtmfStreamAlias = AudioSystem.STREAM_MUSIC;} else {switch (mPlatformType) {case AudioSystem.PLATFORM_VOICE:mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE;dtmfStreamAlias = AudioSystem.STREAM_RING;break;default:mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT;dtmfStreamAlias = AudioSystem.STREAM_MUSIC;}}if (mIsSingleVolume) {mRingerModeAffectedStreams = 0;} else {if (isInCommunication()) {dtmfStreamAlias = AudioSystem.STREAM_VOICE_CALL;mRingerModeAffectedStreams &= ~(1 << AudioSystem.STREAM_DTMF);} else {mRingerModeAffectedStreams |= (1 << AudioSystem.STREAM_DTMF);}}
private final int[] STREAM_VOLUME_ALIAS_TELEVISION = new int[] {AudioSystem.STREAM_MUSIC, // STREAM_VOICE_CALLAudioSystem.STREAM_MUSIC, // STREAM_SYSTEMAudioSystem.STREAM_MUSIC, // STREAM_RINGAudioSystem.STREAM_MUSIC, // STREAM_MUSICAudioSystem.STREAM_MUSIC, // STREAM_ALARMAudioSystem.STREAM_MUSIC, // STREAM_NOTIFICATIONAudioSystem.STREAM_MUSIC, // STREAM_BLUETOOTH_SCOAudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCEDAudioSystem.STREAM_MUSIC, // STREAM_DTMFAudioSystem.STREAM_MUSIC, // STREAM_TTSAudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY};private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] {AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALLAudioSystem.STREAM_RING, // STREAM_SYSTEMAudioSystem.STREAM_RING, // STREAM_RINGAudioSystem.STREAM_MUSIC, // STREAM_MUSICAudioSystem.STREAM_ALARM, // STREAM_ALARMAudioSystem.STREAM_RING, // STREAM_NOTIFICATIONAudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCOAudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCEDAudioSystem.STREAM_RING, // STREAM_DTMFAudioSystem.STREAM_MUSIC, // STREAM_TTSAudioSystem.STREAM_MUSIC // STREAM_ACCESSIBILITY};
AudioService获取到单一音量控制状态后,根据映射来更新系统不同音频流的音量;