引用: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才能被重新加載生效