基于Android Q 修改默认音量等级
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                基于Android Q 修改默认音量等级
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                在之前的一些android版本中,修改默認音量是修改:
path:frameworks\base\media\java\android\media\AudioService.java
/// M: modify the default stream volume @{public static int[] DEFAULT_STREAM_VOLUME = new int[] {4, // STREAM_VOICE_CALL8, // STREAM_SYSTEM8, // STREAM_RING8, // STREAM_MUSIC8, // STREAM_ALARM8, // STREAM_NOTIFICATION8, // STREAM_BLUETOOTH_SCO8, // STREAM_SYSTEM_ENFORCED8, // STREAM_DTMF8, // STREAM_TTS8 // STREAM_ACCESSIBILITY};/// @}當然此文件中也定義了Volume的最大值和最小值:
/** Maximum volume index values for audio streams *//// M: Modify the max stream volume @{protected static int[] MAX_STREAM_VOLUME = new int[] {7, // STREAM_VOICE_CALL15, // STREAM_SYSTEM15, // STREAM_RING15, // STREAM_MUSIC15, // STREAM_ALARM15, // STREAM_NOTIFICATION15, // STREAM_BLUETOOTH_SCO15, // STREAM_SYSTEM_ENFORCED15, // STREAM_DTMF15, // STREAM_TTS15 // STREAM_ACCESSIBILITY};/** Minimum volume index values for audio streams */protected static int[] MIN_STREAM_VOLUME = new int[] {1, // STREAM_VOICE_CALL0, // STREAM_SYSTEM0, // STREAM_RING0, // STREAM_MUSIC1, // STREAM_ALARM0, // STREAM_NOTIFICATION0, // STREAM_BLUETOOTH_SCO0, // STREAM_SYSTEM_ENFORCED0, // STREAM_DTMF0, // STREAM_TTS1 // STREAM_ACCESSIBILITY};OK? 回歸主題,在android Q版本中DEFAULT_STREAM_VOLUME數組的定義是在AudioSystem.java文件中,但是修改這個數組中的值發現還是無法正確處理為自己想要的默認值。
發現初始化默認值是在AudioService.java的AudioService()方法中:
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] =(maxCallVolume * 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];}發現這些初始化的值都是由一些ro.config.屬性控制的,所以要修改默認音量大小,可以直接客制化ro.config.這些屬性值就OK。
總結
以上是生活随笔為你收集整理的基于Android Q 修改默认音量等级的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: LoRa节点开发:4、代码详解 LoRa
- 下一篇: 城市被淹,全都在家
