自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
音效
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
从 Android 11 开始,在选择某个音频设备以将其用于捕获或播放音频时,设备制造商可以自动附加和启用特定的音效。一项重大改进是,完全在音频 HAL(输入设备和输出设备之间的直接连接)之下实现的音频路径上插入的音效现在可以由音效框架控制。
此功能主要面向汽车原始设备制造商 (OEM),但也可用于其他 Android 设备类型。一个示例应用是在通过音频 DSP 直接连接到音响设备时在 FM 调谐器输出中插入语音增强效果。
前提条件
- 至于任何其他音效,效果都必须由供应商库实现,并在
audio_effects.xml
配置文件中列出。
- 效果必须为预处理或后处理类型(
TYPE_PRE_PROC
标志,或在 EffectDescriptor.flags
中设置的 TYPE_POST_PROC
)。
- 如果效果实现经过硬件加速(在
EffectDescriptor.flags
中设置了 HW_ACC_TUNNEL
标志),可以将其附加到完全在 HAL 之下连接的音频路径(在音频 HAL 中没有打开任何播放或捕获音频流)。
创建和启用设备效果
您可以使用以下两种方法之一实例化设备专用音效。
使用音效配置文件
此方法允许以静态方式创建音效,相应音效会被系统性地附加并启用到选择指定设备作为接收器或源的任何音频路径上。
具体方法是在 audio_effects.xml
文件中添加特定的版块,如下所示:
<deviceEffects>
<devicePort type="AUDIO_DEVICE_IN_BUILTIN_MIC" address="bottom">
<apply effect="agc"/>
</devicePort>
</deviceEffects>
使用系统 API
android.media.audiofx.AudioEffect
类中新增了一个 @SystemApi 构造函数,用于创建和启用设备效果:
AudioEffect(@NonNull UUID uuid, @NonNull AudioDeviceAttributes device);
通过指定独一无二的音效 ID 和音频设备描述符来创建效果后,便可使用现有 AudioEffect API 启用或停用该效果。
API 也可以用来查询实现是否支持给定设备/效果的组合。
static boolean isEffectSupportedForDevice(
@NonNull UUID uuid, @NonNull AudioDeviceAttributes device);
新增的 HAL API
音效 HAL
音效 HAL V6.0 的 createEffect()
方法有了新的签名,支持创建附加到特定设备的效果:
IEffectFactory::createEffect(Uuid uid, AudioSession session,
AudioIoHandle ioHandle, AudioPortHandle device)
- 指定的
AudioSession
必须为 AudioSessionConsts.DEVICE
。
- 如果
session
为 AudioSessionConsts.DEVICE
,系统会忽略 AudioIoHandle
。
- 使用
IDevice::createAudioPatch()
方法在音频 HAL 上选择设备时,device
由音频框架向其分配的唯一 AudioPortHandle
进行标识。
音频 HAL
如需支持设备效果功能,音频 HAL 必须使用 IDevice::createAudioPatch()
API 实现音频路由控件。报告 true
的 IDevice::supportsAudioPatches()
方法指明了这一点。
IDevice::addDeviceEffect(AudioPortHandle device, uint64_t effectId)
和 IDevice::removeDeviceEffect(AudioPortHandle device, uint64_t effectId)
这两种新增的 API 方法用于告知 HAL 实现在给定设备上启用或停用了设备效果。
设备由其 AudioPortHandle
ID 标识,此 ID 在通过 IDevice::createAudioPatch()
方法创建音频通路时使用。
如果在启用或停用效果时,音频 HAL 与效果 HAL 之间需要协调,实现可以使用音频 HAL API。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Audio effects\n\nStarting in Android 11, the device manufacturers have the ability\nto automatically attach and enable specific audio effects when a given audio device is\nselected for audio capture or playback. One major improvement is that audio effects inserted\non an audio path entirely implemented below the audio HAL (direct connection between an input\ndevice and an output device) can be controlled by the audio effects framework.\n\n\nThis feature is primarily targeted at automotive OEMs but can also be used in other Android\nform factors. An example app is inserting a voice enhancement effect on the FM tuner\noutput when directly connected to the speaker through the audio DSP.\n\nPrerequisites\n-------------\n\n- As for any other audio effect, the effect must be implemented by a vendor library and listed in the `audio_effects.xml` configuration file.\n- The effect must be of type preprocessing or postprocessing (flag `TYPE_PRE_PROC` or `TYPE_POST_PROC` set in `EffectDescriptor.flags`).\n- If the effect implementation is HW accelerated (flag `HW_ACC_TUNNEL` set in `EffectDescriptor.flags`), it can be attached to an audio path entirely connected below the HAL (no playback or capture audio stream opened at the audio HAL).\n\nCreate and enable a device effect\n---------------------------------\n\n\nDevice-specific audio effects can be instantiated using one of the two methods below.\n\n### Use an audio effects configuration file\n\n\nThis method allows for the static creation of an audio effect that is systematically attached\nand enabled to any audio path selecting a specified device as sink or source.\n\n\nThis is done by adding a specific section in the `audio_effects.xml`\nfile as follows: \n\n```carbon\n\u003cdeviceEffects\u003e\n\u003cdevicePort type=\"AUDIO_DEVICE_IN_BUILTIN_MIC\" address=\"bottom\"\u003e\n \t\u003capply effect=\"agc\"/\u003e\n \u003c/devicePort\u003e\n \u003c/deviceEffects\u003e\n \n```\n\n### Use a system API\n\n\nA new @SystemApi constructor has been added to the\n[`android.media.audiofx.AudioEffect`](https://developer.android.com/reference/android/media/audiofx/AudioEffect) class to create and enable a device effect: \n\n```transact-sql\nAudioEffect(@NonNull UUID uuid, @NonNull AudioDeviceAttributes device);\n```\n\n\nAfter the effect is created by specifying the unique audio effect ID and audio device descriptor,\nit can be enabled or disabled with existing AudioEffect APIs.\n\n\nAn API is also available to query if an implementation supports a given device/effect combination. \n\n```transact-sql\nstatic boolean isEffectSupportedForDevice(\n @NonNull UUID uuid, @NonNull AudioDeviceAttributes device);\n```\n\nNew HAL APIs\n------------\n\n### Audio effect HAL\n\n\nThe audio effect HAL V6.0 has a new signature for the `createEffect()` method\nallowing the creation of an effect attached to a device: \n\n```text\nIEffectFactory::createEffect(Uuid uid, AudioSession session,\nAudioIoHandle ioHandle, AudioPortHandle device)\n```\n\n- The `AudioSession` specified must be `AudioSessionConsts.DEVICE`.\n- `AudioIoHandle` is ignored if the `session` is `AudioSessionConsts.DEVICE`.\n- The `device` is identified by its unique `AudioPortHandle\n ` assigned by the audio framework when the device is selected at the audio HAL with `IDevice::createAudioPatch()` method.\n\n### Audio HAL\n\n\nTo support the device effect feature, the audio HAL must implement audio routing\ncontrol using the `IDevice::createAudioPatch()` API. This is indicated by the\n`IDevice::supportsAudioPatches()` method reporting `true`.\n\nTwo new API methods,\n`IDevice::addDeviceEffect(AudioPortHandle device, uint64_t effectId)` and\n`IDevice::removeDeviceEffect(AudioPortHandle device, uint64_t effectId)`\ntell the HAL implementation that a device effect has been enabled or disabled on\na given device.\n\n\nThe device is identified by its `AudioPortHandle` ID, which is used when an audio\npatch is created with the `IDevice::createAudioPatch()` method.\n\n\nThe Audio HAL APIs can be used by an implementation if coordination is needed between the\naudio and effect HALs when an effect is enabled or disabled."]]