当前位置:   article > 正文

Android 12 修改系统音量默认初始值_android 默认音量

android 默认音量

1.需求:Android 系统出厂默认的音量值过小,需要把音量默认初始值改成音量的最大值.

2.涉及核心代码:

  1. frameworks/base/media/java/android/media/AudioSystem.java
  2. frameworks/base/services/core/java/com/android/server/audio/AudioService.java

3.系统音量默认初始值在AudioSystem.java中定义,代码如下:

  1. /** @hide */
  2. public static int[] DEFAULT_STREAM_VOLUME = new int[] {
  3. 4, // STREAM_VOICE_CALL
  4. 7, // STREAM_SYSTEM
  5. 5, // STREAM_RING
  6. 5, // STREAM_MUSIC
  7. 6, // STREAM_ALARM
  8. 5, // STREAM_NOTIFICATION
  9. 7, // STREAM_BLUETOOTH_SCO
  10. 7, // STREAM_SYSTEM_ENFORCED
  11. 5, // STREAM_DTMF
  12. 5, // STREAM_TTS
  13. 5, // STREAM_ACCESSIBILITY
  14. 5, // STREAM_ASSISTANT
  15. };

3.系统音量最大值和最小值定义在AudioService.java 中,代码如下:

  1. /** Maximum volume index values for audio streams */
  2. protected static int[] MAX_STREAM_VOLUME = new int[] {
  3. 5, // STREAM_VOICE_CALL
  4. 7, // STREAM_SYSTEM
  5. 7, // STREAM_RING
  6. 15, // STREAM_MUSIC
  7. 7, // STREAM_ALARM
  8. 7, // STREAM_NOTIFICATION
  9. 15, // STREAM_BLUETOOTH_SCO
  10. 7, // STREAM_SYSTEM_ENFORCED
  11. 15, // STREAM_DTMF
  12. 15, // STREAM_TTS
  13. 15, // STREAM_ACCESSIBILITY
  14. 15 // STREAM_ASSISTANT
  15. };
  16. /** Minimum volume index values for audio streams */
  17. protected static int[] MIN_STREAM_VOLUME = new int[] {
  18. 1, // STREAM_VOICE_CALL
  19. 0, // STREAM_SYSTEM
  20. 0, // STREAM_RING
  21. 0, // STREAM_MUSIC
  22. 1, // STREAM_ALARM
  23. 0, // STREAM_NOTIFICATION
  24. 0, // STREAM_BLUETOOTH_SCO
  25. 0, // STREAM_SYSTEM_ENFORCED
  26. 0, // STREAM_DTMF
  27. 0, // STREAM_TTS
  28. 1, // STREAM_ACCESSIBILITY
  29. 0 // STREAM_ASSISTANT
  30. };

------------------------------------------------------------修改方案一---------------------------------------------

在AudioSystem.Java中,修改把默认的音量值改成需要的值,如:把设置里音量中的"媒体音量","通话音量","铃声和通知音量","闹钟音量" 改成最大值. 

(1)第一步:对照AudioService.java中的MAX_STREAM_VOLUME数组中的值做相应的修改.

  1. /** @hide */
  2. public static int[] DEFAULT_STREAM_VOLUME = new int[] {
  3. 5, // STREAM_VOICE_CALL
  4. 7, // STREAM_SYSTEM
  5. 7, // STREAM_RING
  6. 15, // STREAM_MUSIC
  7. 7, // STREAM_ALARM
  8. 7, // STREAM_NOTIFICATION
  9. 15, // STREAM_BLUETOOTH_SCO
  10. 7, // STREAM_SYSTEM_ENFORCED
  11. 15, // STREAM_DTMF
  12. 15, // STREAM_TTS
  13. 15, // STREAM_ACCESSIBILITY
  14. 15, // STREAM_ASSISTANT
  15. };

(2)步骤二:由于AudioService.java在构造函数中,会覆盖最大值和默认初始值,所以需要把构造函数中从配置文件中取值的代码注释掉,才能让步骤一中修改的数组的值起作用,最后清楚系统数据后便会生效.

  1. /** @hide */
  2. public AudioService(Context context) {
  3. this(context, AudioSystemAdapter.getDefaultAdapter(),
  4. SystemServerAdapter.getDefaultAdapter(context));
  5. }
  6. public AudioService(Context context, AudioSystemAdapter audioSystem,
  7. SystemServerAdapter systemServer) {
  8. sLifecycleLogger.log(new AudioEventLogger.StringEvent("AudioService()"));
  9. mContext = context;
  10. mContentResolver = context.getContentResolver();
  11. mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
  12. mAudioSystem = audioSystem;
  13. mSystemServer = systemServer;
  14. mPlatformType = AudioSystem.getPlatformType(context);
  15. mIsSingleVolume = AudioSystem.isSingleVolume(context);
  16. mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
  17. mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
  18. mSensorPrivacyManagerInternal =
  19. LocalServices.getService(SensorPrivacyManagerInternal.class);
  20. PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
  21. mAudioEventWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "handleAudioEvent");
  22. mSfxHelper = new SoundEffectsHelper(mContext);
  23. mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  24. mHasVibrator = mVibrator == null ? false : mVibrator.hasVibrator();
  25. mSupportsMicPrivacyToggle = context.getSystemService(SensorPrivacyManager.class)
  26. .supportsSensorToggle(SensorPrivacyManager.Sensors.MICROPHONE);
  27. mUseVolumeGroupAliases = mContext.getResources().getBoolean(
  28. com.android.internal.R.bool.config_handleVolumeAliasesUsingVolumeGroups);
  29. // Initialize volume
  30. // Priority 1 - Android Property
  31. // Priority 2 - Audio Policy Service
  32. // Priority 3 - Default Value
  33. if (AudioProductStrategy.getAudioProductStrategies().size() > 0) {
  34. int numStreamTypes = AudioSystem.getNumStreamTypes();
  35. for (int streamType = numStreamTypes - 1; streamType >= 0; streamType--) {
  36. AudioAttributes attr =
  37. AudioProductStrategy.getAudioAttributesForStrategyWithLegacyStreamType(
  38. streamType);
  39. int maxVolume = AudioSystem.getMaxVolumeIndexForAttributes(attr);
  40. if (maxVolume != -1) {
  41. MAX_STREAM_VOLUME[streamType] = maxVolume;
  42. }
  43. int minVolume = AudioSystem.getMinVolumeIndexForAttributes(attr);
  44. if (minVolume != -1) {
  45. MIN_STREAM_VOLUME[streamType] = minVolume;
  46. }
  47. }
  48. if (mUseVolumeGroupAliases) {
  49. // Set all default to uninitialized.
  50. for (int stream = 0; stream < AudioSystem.DEFAULT_STREAM_VOLUME.length; stream++) {
  51. AudioSystem.DEFAULT_STREAM_VOLUME[stream] = UNSET_INDEX;
  52. }
  53. }
  54. }
  55. /*注释开始
  56. int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1);
  57. if (maxCallVolume != -1) {
  58. MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume;
  59. }
  60. int defaultCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_default", -1);
  61. if (defaultCallVolume != -1 &&
  62. defaultCallVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] &&
  63. defaultCallVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) {
  64. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = defaultCallVolume;
  65. } else {
  66. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] =
  67. (MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] * 3) / 4;
  68. }
  69. int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1);
  70. if (maxMusicVolume != -1) {
  71. MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume;
  72. }
  73. int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1);
  74. if (defaultMusicVolume != -1 &&
  75. defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] &&
  76. defaultMusicVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) {
  77. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume;
  78. } else {
  79. if (isPlatformTelevision()) {
  80. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] =
  81. MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4;
  82. } else {
  83. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] =
  84. MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3;
  85. }
  86. }
  87. int maxAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_steps", -1);
  88. if (maxAlarmVolume != -1) {
  89. MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = maxAlarmVolume;
  90. }
  91. int defaultAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_default", -1);
  92. if (defaultAlarmVolume != -1 &&
  93. defaultAlarmVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]) {
  94. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = defaultAlarmVolume;
  95. } else {
  96. // Default is 6 out of 7 (default maximum), so scale accordingly.
  97. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] =
  98. 6 * MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] / 7;
  99. }
  100. int maxSystemVolume = SystemProperties.getInt("ro.config.system_vol_steps", -1);
  101. if (maxSystemVolume != -1) {
  102. MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = maxSystemVolume;
  103. }
  104. int defaultSystemVolume = SystemProperties.getInt("ro.config.system_vol_default", -1);
  105. if (defaultSystemVolume != -1 &&
  106. defaultSystemVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]) {
  107. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = defaultSystemVolume;
  108. } else {
  109. // Default is to use maximum.
  110. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] =
  111. MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM];
  112. }
  113. 注释结束*/
  114. createAudioSystemThread();
  115. AudioSystem.setErrorCallback(mAudioSystemCallback);
  116. updateAudioHalPids();
  117. boolean cameraSoundForced = readCameraSoundForced();
  118. mCameraSoundForced = new Boolean(cameraSoundForced);
  119. sendMsg(mAudioHandler,
  120. MSG_SET_FORCE_USE,
  121. SENDMSG_QUEUE,
  122. AudioSystem.FOR_SYSTEM,
  123. cameraSoundForced ?
  124. AudioSystem.FORCE_SYSTEM_ENFORCED : AudioSystem.FORCE_NONE,
  125. new String("AudioService ctor"),
  126. 0);
  127. mSafeMediaVolumeState = Settings.Global.getInt(mContentResolver,
  128. Settings.Global.AUDIO_SAFE_VOLUME_STATE,
  129. SAFE_MEDIA_VOLUME_NOT_CONFIGURED);
  130. // The default safe volume index read here will be replaced by the actual value when
  131. // the mcc is read by onConfigureSafeVolume()
  132. mSafeMediaVolumeIndex = mContext.getResources().getInteger(
  133. com.android.internal.R.integer.config_safe_media_volume_index) * 10;
  134. mUseFixedVolume = mContext.getResources().getBoolean(
  135. com.android.internal.R.bool.config_useFixedVolume);
  136. mDeviceBroker = new AudioDeviceBroker(mContext, this);
  137. mRecordMonitor = new RecordingActivityMonitor(mContext);
  138. mRecordMonitor.registerRecordingCallback(mVoiceRecordingActivityMonitor, true);
  139. // must be called before readPersistedSettings() which needs a valid mStreamVolumeAlias[]
  140. // array initialized by updateStreamVolumeAlias()
  141. updateStreamVolumeAlias(false /*updateVolumes*/, TAG);
  142. readPersistedSettings();
  143. readUserRestrictions();
  144. mPlaybackMonitor =
  145. new PlaybackActivityMonitor(context, MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]);
  146. mPlaybackMonitor.registerPlaybackCallback(mVoicePlaybackActivityMonitor, true);
  147. mMediaFocusControl = new MediaFocusControl(mContext, mPlaybackMonitor);
  148. readAndSetLowRamDevice();
  149. mIsCallScreeningModeSupported = AudioSystem.isCallScreeningModeSupported();
  150. if (mSystemServer.isPrivileged()) {
  151. LocalServices.addService(AudioManagerInternal.class, new AudioServiceInternal());
  152. mUserManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener);
  153. mRecordMonitor.initMonitor();
  154. }
  155. mMonitorRotation = SystemProperties.getBoolean("ro.audio.monitorRotation", false);
  156. // done with service initialization, continue additional work in our Handler thread
  157. queueMsgUnderWakeLock(mAudioHandler, MSG_INIT_STREAMS_VOLUMES,
  158. 0 /* arg1 */, 0 /* arg2 */, null /* obj */, 0 /* delay */);
  159. }

--------------------------------------修改方案二---------------------------------------------

由于AudioService.java构造函数中会从配置文件中对音量默认值和音量最大值再次赋值,

  1. int maxCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_steps", -1);
  2. if (maxCallVolume != -1) {
  3. MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = maxCallVolume;
  4. }
  5. //从系统属性"ro.config.vc_call_vol_default"中获取默认电话音量
  6. int defaultCallVolume = SystemProperties.getInt("ro.config.vc_call_vol_default", -1);
  7. if (defaultCallVolume != -1 &&
  8. defaultCallVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] &&
  9. defaultCallVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]) {
  10. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = defaultCallVolume;
  11. } else {
  12. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] =
  13. (MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] * 3) / 4;
  14. }
  15. int maxMusicVolume = SystemProperties.getInt("ro.config.media_vol_steps", -1);
  16. if (maxMusicVolume != -1) {
  17. MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = maxMusicVolume;
  18. }
  19. //从系统属性"ro.config.media_vol_default"中获取默认媒体音量
  20. int defaultMusicVolume = SystemProperties.getInt("ro.config.media_vol_default", -1);
  21. if (defaultMusicVolume != -1 &&
  22. defaultMusicVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] &&
  23. defaultMusicVolume >= MIN_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]) {
  24. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = defaultMusicVolume;
  25. } else {
  26. if (isPlatformTelevision()) {
  27. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] =
  28. MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 4;
  29. } else {
  30. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] =
  31. MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] / 3;
  32. }
  33. }
  34. int maxAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_steps", -1);
  35. if (maxAlarmVolume != -1) {
  36. MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = maxAlarmVolume;
  37. }
  38. //从系统属性"ro.config.alarm_vol_default"中获取默认闹钟和通知音量
  39. int defaultAlarmVolume = SystemProperties.getInt("ro.config.alarm_vol_default", -1);
  40. if (defaultAlarmVolume != -1 &&
  41. defaultAlarmVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM]) {
  42. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] = defaultAlarmVolume;
  43. } else {
  44. // Default is 6 out of 7 (default maximum), so scale accordingly.
  45. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_ALARM] =
  46. 6 * MAX_STREAM_VOLUME[AudioSystem.STREAM_ALARM] / 7;
  47. }
  48. int maxSystemVolume = SystemProperties.getInt("ro.config.system_vol_steps", -1);
  49. if (maxSystemVolume != -1) {
  50. MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = maxSystemVolume;
  51. }
  52. //从系统属性"ro.config.system_vol_default"中获取默认系统音量
  53. int defaultSystemVolume = SystemProperties.getInt("ro.config.system_vol_default", -1);
  54. if (defaultSystemVolume != -1 &&
  55. defaultSystemVolume <= MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM]) {
  56. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] = defaultSystemVolume;
  57. } else {
  58. // Default is to use maximum.
  59. AudioSystem.DEFAULT_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM] =
  60. MAX_STREAM_VOLUME[AudioSystem.STREAM_SYSTEM];
  61. }

所以可以在配置文件中添加对应的属性,并赋值,这样就不需要修改代码.默认Android 源码中并没有给这些属性配值,所以直接添加上去就可以了.其中,配置文件在设备的 /system/build.prop .

  1. <property name="ro.config.vc_call_vol_default" value="5">
  2. <property name="ro.config.media_vol_default" value="15">
  3. <property name="ro.config.alarm_vol_default" value="7">
  4. <property name="ro.config.system_vol_default" value="7">

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/387931
推荐阅读
相关标签
  

闽ICP备14008679号