Android系统拥有多种声音属性,包括有多媒体、通话、通知等声音属性,这些声音属性分别含有自己默认音量、最大音量和最小音量属性。我们看下Android10系统源码对此的定义和设置。
音量设置和定义都在frameworks/base/media/java/android/media/AudioSystem.java中。具体如下:
系统声音的最大音量和最小音量则定义在
frameworks/base/services/core/java/com/android/server/audio/AudioService.java 中,具体如下:
/** Maximum volume index values for audio streams */
protected static int[] MAX_STREAM_VOLUME = new int[] {
5, // STREAM_VOICE_CALL
7, // STREAM_SYSTEM
7, // STREAM_RING
15, // STREAM_MUSIC
7, // STREAM_ALARM
7, // STREAM_NOTIFICATION
15, // STREAM_BLUETOOTH_SCO
7, // STREAM_SYSTEM_ENFORCED
15, // STREAM_DTMF
15, // STREAM_TTS
15 // STREAM_ACCESSIBILITY
};/** Minimum volume index values for audio streams */
protected static int[] MIN_STREAM_VOLUME = new int[] {
1, // STREAM_VOICE_CALL
0, // STREAM_SYSTEM
0, // STREAM_RING
0, // STREAM_MUSIC
1, // STREAM_ALARM
0, // STREAM_NOTIFICATION
0, // STREAM_BLUETOOTH_SCO
0, // STREAM_SYSTEM_ENFORCED
0, // STREAM_DTMF
0, // STREAM_TTS
1 // STREAM_ACCESSIBILITY
};
通过上面的介绍,虽然我们知道系统声音的三种音量值的定义位置,但是声音属性 默认音量和最大音量最好不要在这里进行修改,否则你的修改可能会是无效。你可能会问为什么?
因为这两种音量属性被定义之后,还会在 AudioService.java 的构造方法中进行初始化。在此期间,之前定义的音量值可能会被覆盖掉。
// framework/base/services/core/java/com/android/server/audio/AudioService.java
/** @hide */
public AudioService(Context context) {
//.....省略部分代码
int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1);
if (maxCallVolume != -1) {
MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume;
}
int defaultCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_default", -1);
if (defaultCallVolume != -1 &&
defaultCallVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] &&
defaultCallVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = defaultCallVolume;
} else {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] =
(MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] * 3) / 4;
}
int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1);
if (maxMusicVolume != -1) {
MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume;
}
int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1);
if (defaultMusicVolume != -1 &&
defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] &&
defaultMusicVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume;
} else {
if (isPlatformTelevision()) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] =
MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4;
} else {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] =
MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3;
}
}
int maxAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_steps", -1);
if (maxAlarmVolume != -1) {
MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = maxAlarmVolume;
}
int defaultAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_default", -1);
if (defaultAlarmVolume != -1 &&
defaultAlarmVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = defaultAlarmVolume;
} else {
// Default is 6 out of 7 (default maximum), so scale accordingly.
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] =
6 * MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] / 7;
}
int maxSystemVolume = SystemProperties.getInt("ro.config.system_vol_steps", -1);
if (maxSystemVolume != -1) {
MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = maxSystemVolume;
}
int defaultSystemVolume = SystemProperties.getInt("ro.config.system_vol_default", -1);
if (defaultSystemVolume != -1 &&
defaultSystemVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = defaultSystemVolume;
} else {
// Default is to use maximum.
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] =
MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM];
}
//UNISOC:bug992603 custom default volume for alarm ring and notification stream.
int defaultRingVolume = SystemProperties.getInt("ro.config.ring_vol_default", -1);
if (defaultRingVolume != -1 &&
defaultRingVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_RING]) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_RING] = defaultRingVolume;
}
int defaultNotificationVolume = SystemProperties.getInt("ro.config.notification_vol_default", -1);
if (defaultNotificationVolume != -1 &&
defaultNotificationVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_NOTIFICATION]) {
AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_NOTIFICATION] = defaultNotificationVolume;
}
//..... 省略代码
}
以上我们看到不同的音量默认值都是从配置文件中读取,然后给变量赋值的。我们以默认铃声音量为例修改。
如果我们修改某个值个人建议有如下方案:
方案一:在AudioManager中修改DEFAULT_STREAM_VOLUME中对应的类型默认值,在audioService构造方法中注释该值赋值逻辑
方案二:在audioService中根据逻辑赋值
方案三:修改编译配置文件,配置默认值(参考。该方案未作验证,仅供参考)
因篇幅问题不能全部显示,请点此查看更多更全内容