引用:Android 音频策略配置文件解析流程
audio_policy_configuration.xml 是 Android 音频系统的核心配置文件,它定义了音频硬件接口、设备路由和基本策略。下面我将详细介绍这个文件的结构、关键配置项和实际应用。audio_policy_configuration.xml 是 Android 音频系统的核心配置文件,它定义了音频硬件接口、设备路由和基本策略。下面将详细介绍这个文件的结构、关键配置项和实际应用。
1. 文件基本结构
<audioPolicyConfiguration version="1.0">
<globalConfiguration
speaker_drc_enabled="true"
call_volume_behavior="1"/>
<modules>
<!-- 音频模块配置 -->
<module name="primary" halVersion="3.0">
<!-- 设备、接口和路由配置 -->
</module>
<module name="usb" halVersion="3.0">
<!-- USB音频模块配置 -->
</module>
</modules>
</audioPolicyConfiguration>
2. 主要配置部分详解
(1)globalConfiguration(全局配置)
speaker_drc_enabled 是否启用扬声器动态范围控制 true/false
call_volume_behavior 通话音量行为模式 0-2
volume_down_mute 音量减到最低时是否静音 true/false
(2)modules(音频模块)
每个module代表一个音频硬件子系统:
<module name="primary" halVersion="3.0">
<attachedDevices>
<item>Speaker</item>
<item>Built-In Mic</item>
</attachedDevices>
<defaultOutputDevice>Speaker</defaultOutputDevice>
<mixPorts>
<!-- 混音端口配置 -->
</mixPorts>
<devicePorts>
<!-- 物理设备端口配置 -->
</devicePorts>
<routes>
<!-- 路由规则配置 -->
</routes>
</module>
attachedDevices 默认连接的设备,常见的类型:Speaker、Built-In Mic、Wired Headset等
mixPorts 混音端口
定义音频处理接口:
<mixPort name="primary output" role="source" flags="AUDIO_OUTPUT_FLAG_PRIMARY">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</mixPort>
<mixPort name="record_24bit" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED"
samplingRates="48000,96000" channelMasks="AUDIO_CHANNEL_IN_STEREO"/>
</mixPort>
- role:source(输出)/sink(输入)
- flags:特殊标志,如AUDIO_OUTPUT_FLAG_PRIMARY(主输出)
- profile:支持的音频格式、采样率和声道配置
devicePorts(设备端口)
定义物理音频设备:
<devicePort tagName="Speaker" type="AUDIO_DEVICE_OUT_SPEAKER" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</devicePort>
<devicePort tagName="Headphones" type="AUDIO_DEVICE_OUT_WIRED_HEADPHONE" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</devicePort>
常见设备类型:
- 输出设备:AUDIO_DEVICE_OUT_SPEAKER、AUDIO_DEVICE_OUT_WIRED_HEADPHONE
- 输入设备:AUDIO_DEVICE_IN_BUILTIN_MIC、AUDIO_DEVICE_IN_WIRED_HEADSET
routes(路由规则)
定义音频流如何从mixPort路由到devicePort:
<routes>
<route type="mix" sink="Speaker" sources="primary output"/>
<route type="mix" sink="Headphones" sources="primary output"/>
<route type="mix" sink="record_24bit" sources="Built-In Mic"/>
</routes>
3. 完整配置示例
<audioPolicyConfiguration version="1.0">
<globalConfiguration
speaker_drc_enabled="true"
call_volume_behavior="1"/>
<modules>
<module name="primary" halVersion="3.0">
<attachedDevices>
<item>Speaker</item>
<item>Built-In Mic</item>
</attachedDevices>
<defaultOutputDevice>Speaker</defaultOutputDevice>
<mixPorts>
<mixPort name="primary output" role="source" flags="AUDIO_OUTPUT_FLAG_PRIMARY">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</mixPort>
<mixPort name="deep_buffer" role="source" flags="AUDIO_OUTPUT_FLAG_DEEP_BUFFER">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</mixPort>
<mixPort name="record_24bit" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_24_BIT_PACKED"
samplingRates="48000,96000" channelMasks="AUDIO_CHANNEL_IN_STEREO"/>
</mixPort>
</mixPorts>
<devicePorts>
<devicePort tagName="Speaker" type="AUDIO_DEVICE_OUT_SPEAKER" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</devicePort>
<devicePort tagName="Headphones" type="AUDIO_DEVICE_OUT_WIRED_HEADPHONE" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</devicePort>
<devicePort tagName="Built-In Mic" type="AUDIO_DEVICE_IN_BUILTIN_MIC" role="source">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="48000" channelMasks="AUDIO_CHANNEL_IN_MONO"/>
</devicePort>
</devicePorts>
<routes>
<route type="mix" sink="Speaker" sources="primary output,deep_buffer"/>
<route type="mix" sink="Headphones" sources="primary output,deep_buffer"/>
<route type="mix" sink="record_24bit" sources="Built-In Mic"/>
</routes>
</module>
</modules>
</audioPolicyConfiguration>
4. 常见配置场景
(1)添加USB音频支持
<module name="usb" halVersion="3.0">
<attachedDevices>
<item>USB Device</item>
</attachedDevices>
<mixPorts>
<mixPort name="usb output" role="source">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="44100,48000,96000"
channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</mixPort>
</mixPorts>
<devicePorts>
<devicePort tagName="USB Device" type="AUDIO_DEVICE_OUT_USB_DEVICE" role="sink">
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="44100,48000,96000"
channelMasks="AUDIO_CHANNEL_OUT_STEREO"/>
</devicePort>
</devicePorts>
<routes>
<route type="mix" sink="USB Device" sources="usb output"/>
</routes>
</module>
(2)配置多采样率支持
<profile name="" format="AUDIO_FORMAT_PCM_16_BIT"
samplingRates="44100,48000,96000,192000"
channelMasks="AUDIO_CHANNEL_OUT_STEREO,AUDIO_CHANNEL_OUT_5POINT1"/>
5. 调试技巧
- 查看当前配置:
adb shell dumpsys media.audio_policy
- 检查音频路由:
adb shell dumpsys audio
- 验证配置文件:
adb shell ls -l /vendor/etc/audio_policy_configuration.xml
注:大多数厂商都有自定义的配置文件,不使用vendor下的原生文件
- 重新加载配置(需要root):
adb shell killall audioserver
audio_policy_configuration.xml配置文件修改后,必须重启audioserver才能被重新加载生效