自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
音效
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
自 Android 11 起,裝置製造商在選取特定音訊裝置進行音訊擷取或播放時,可以自動附加並啟用特定音訊效果。其中一個重大改善項目是,在音訊路徑中插入的音訊特效 (完全在音訊 HAL 下方實作) 可由音訊特效架構控制。
這項功能主要針對汽車原始設備製造商 (OEM) 設計,但也可以用於其他 Android 板型規格。當透過音訊 DSP 直接連線至揚聲器時,應用程式會在 FM 調諧器輸出內容中插入語音強化效果。
必要條件
- 至於其他音效,則必須由供應商程式庫實作,並列於
audio_effects.xml
設定檔中。
- 特效必須是預處理或後處理類型 (在
EffectDescriptor.flags
中設定標記 TYPE_PRE_PROC
或 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
已將新的 @SystemApi 建構函式新增至
android.media.audiofx.AudioEffect
類別,以建立及啟用裝置效果:
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()
方法表示。
兩個新的 API 方法 IDevice::addDeviceEffect(AudioPortHandle device, uint64_t effectId)
和 IDevice::removeDeviceEffect(AudioPortHandle device, uint64_t effectId)
會告知 HAL 實作,在特定裝置上已啟用或停用裝置效果。
裝置會透過 AudioPortHandle
ID 識別,這項 ID 會在使用 IDevice::createAudioPatch()
方法建立音訊修補程式時使用。
如果在啟用或停用效果時,需要在音訊和效果 HAL 之間進行協調,則實作項目可以使用 Audio HAL API。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-29 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2025-07-29 (世界標準時間)。"],[],[],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."]]