1、调用逻辑
安卓java部分的音量设置首先调用到AudioManager.java中
在这里有两种方法可以设置音量setStreamVolume和adjustStreamVolume
setStreamVolume:传入index直接设置音量等级
adjustStreamVolume:传入direction,根据direction和获取到的步长设置音量。
这2中方式都会call到native 层的AudioPolicyManager.cpp中的setStreamVolumeIndex()。
2、分析
status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream,
                                                  int index,
                                                  audio_devices_t device)
{
            ........
            if (applyVolume) {
                //FIXME: workaround for truncated touch sounds
                // delayed volume change for system stream to be removed when the problem is
                // handled by system UI
                //调用checkAndSetVolume设置音量
                status_t volStatus =
                        checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice,
                            (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0);
                if (volStatus != NO_ERROR) {
                    status = volStatus;
                }
            }
}
3、调整音量曲线有2种方式:
(1)8.0以前老版本修改code方式
(2)Android 8.0往后的版本,使用xml的方式
具体参考下面链接:
Android8.0的音量曲线设置_m0_37437363的博客-CSDN博客_音量曲线
4、在audio_policy_configuration.xml里include了下面2个xml
 <xi:include href="audio_policy_volumes.xml"/>
    <xi:include href="default_volume_tables.xml"/>
举例:音量等级index 0~100,对应的衰减量-4200~0 (0是不衰减)
<volumes>
    <volume stream="AUDIO_STREAM_VOICE_CALL" deviceCategory="DEVICE_CATEGORY_HEADSET">
        <point>0,-4200</point>
        <point>33,-2800</point>
        <point>66,-1400</point>
        <point>100,0</point>
    </volume>
5、音量音量
java层实际上调节的是挂在stream上的device的index值,挂在stream上的每个device都有一个音量曲线,native层是将index值转化为音量曲线中的值:
                |--> device1 (index) --> curve1
                |--> device2 (index) --> curve2
    stream1 --> |--> device3 (index) --> curve3
                |--> device4 (index) --> curve4
                |--> device5 (index) --> curve5
                
                |--> device1 (index) --> curve6
                |--> device2 (index) --> curve7
    stream2 --> |--> device3 (index) --> curve8
                |--> device4 (index) --> curve9
                |--> device5 (index) --> curve10
6、如何找对应的音量曲线