自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
音訊延遲 (適用於應用程式開發人員)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
為盡可能降低音訊延遲,建議您使用 Oboe。
Oboe
Oboe 使用原生音訊,並針對 Android 8.1 以上版本使用 AAudio,針對較舊的 Android 版本使用 OpenSL ES。如要讓裝置的延遲時間縮到最短,請設定 setPerformanceMode(oboe::PerformanceMode::LowLatency)
和 setSharingMode(oboe::SharingMode::Exclusive)
。如要進一步瞭解 Oboe,請參閱入門指南、README.md
和完整指南。如需使用 Oboe 的範例應用程式,請參閱 Soundboard 應用程式。
注意:如果您使用 AudioTrack 在 Java 中建立輸出音訊串流,以便為延遲敏感的應用程式提供服務,請使用 PERFORMANCE_MODE_LOW_LATENCY
模式。
導入檢查清單
如要使用 Android 原生音訊:
-
下載並安裝 Android NDK。
-
請參閱 Oboe 的入門指南。
-
在 Oboe 建構工具上呼叫
setPerformanceMode(oboe::PerformanceMode::LowLatency)
和 setSharingMode(oboe::SharingMode::Exclusive)
。
-
使用 android.media.AudioManager.getProperty(java.lang.String) 傳回的建議原生緩衝區大小和取樣率。
注意:輸入內容也應使用相同的緩衝區大小和取樣率。
-
請確保回呼處理常式簡短,且不會造成 CPU 使用量激增或無限阻斷。避免優先順序反轉。
-
建議您使用非阻斷演算法,在輸入和輸出回呼處理常式之間,以及回呼處理常式與其他應用程式之間進行通訊。
其他資源
source.android.com
source.android.com 網站主要適用於建構 Android 裝置的原始設備製造商 (OEM),以及向這些 OEM 供應元件的 SoC 供應商。
不過,這個網站提供許多有關延遲的實用資訊,建議您查看。請參閱「音訊延遲」一文。
android-ndk
如果您對如何使用 Android 原生音訊有任何疑問,歡迎前往討論群組 android-ndk 提問。
在 Oboe 上回報錯誤
如果您在使用 Oboe 時遇到問題,可以回報錯誤。
影片
- 開始使用 Oboe
- 低延遲音訊:讓你聽得更清楚
- 2018 年 Android 開發人員高峰會。
- 在 Android 平台上勝出 - 如何最佳化 Android 音訊應用程式
- 2018 年 Android 開發人員挑戰賽。
- Android 上的高效能音訊 (2013 年 Google I/O 大會)
- 整部影片都是關於延遲時間。
- 在 Android 上打造出色的多媒體體驗 (Google I/O 2014)
- 前 14 分鐘會介紹一般音訊和輸入延遲。
- 音訊延遲:緩衝區大小 (100 Days of Google Dev)
- 說明音訊延遲、緩衝區大小和工作排程之間的關係。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間:2025-03-26 (世界標準時間)。"],[],[],null,["# Audio latency for app developers\n\nFor the lowest possible audio latency, we recommend you use [Oboe](https://developer.android.com/games/sdk/oboe).\n\nOboe\n----\n\n[Oboe](https://github.com/google/oboe) uses native audio and is built\non AAudio for Android 8.1+ and OpenSL ES for older versions of Android. To achieve the lowest\npossible latency for a device, set `setPerformanceMode(oboe::PerformanceMode::LowLatency)`\nand `setSharingMode(oboe::SharingMode::Exclusive)`.\nSee the [Getting started guide](https://github.com/google/oboe/blob/main/docs/GettingStarted.md),\n[`README.md`](https://github.com/google/oboe/blob/main/README.md),\nand [Full guide](https://github.com/google/oboe/blob/main/docs/FullGuide.md) for more\ninformation on Oboe. Refer to the [Soundboard app](https://github.com/google/oboe/tree/main/samples/SoundBoard)\nfor a sample app using Oboe.\n\n**Note:** If you use [AudioTrack](https://developer.android.com/reference/android/media/AudioTrack)\nfor creating output audio streams in Java for latency sensitive applications, use the [`PERFORMANCE_MODE_LOW_LATENCY`](https://developer.android.com/reference/android/media/AudioTrack#PERFORMANCE_MODE_LOW_LATENCY)\nmode.\n\n\u003cbr /\u003e\n\nImplementation checklist\n------------------------\n\nTo use Android native audio:\n\n1. Download and install the [Android NDK](https://developer.android.com/tools/sdk/ndk/index.html).\n2. Follow the [Getting started guide](https://github.com/google/oboe/blob/main/docs/GettingStarted.md) for Oboe.\n3. Call `setPerformanceMode(oboe::PerformanceMode::LowLatency)` and `setSharingMode(oboe::SharingMode::Exclusive)` on the Oboe builder.\n4. Use the recommended native buffer size and sample rate returned by [android.media.AudioManager.getProperty(java.lang.String)](http://developer.android.com/reference/android/media/AudioManager.html#getProperty(java.lang.String)).\n\n **Note:** The same buffer size and sample rate should also be used for input.\n5. Keep your callback handlers short, without bursty CPU usage or unbounded blocking. Avoid [priority inversion.](/docs/core/audio/avoiding_pi)\n6. Consider using [non-blocking algorithms](/docs/core/audio/avoiding_pi#nonBlockingAlgorithms) to communicate between input and output callback handlers, and between the callback handlers and the rest of your application.\n\nOther resources\n---------------\n\n### source.android.com\n\n\nThe site [source.android.com](/)\nis primarily designed for OEMs building Android\ndevices, and the SoC vendors who supply components to these OEMs.\n\n\nHowever, there is a wealth of useful information about latency at this site, so\nyou may want to review it. See the articles at\n[Audio latency.](/docs/core/audio/latency/latency)\n\n### android-ndk\n\n\nIf you have questions about how to use Android native audio, you can ask at the discussion group\n[android-ndk](https://groups.google.com/forum/#!forum/android-ndk).\n\n### Report bugs on Oboe\n\n\nIf you have issues using Oboe, you can\n[file a bug](https://github.com/google/oboe/issues).\n\n### Videos\n\n[Getting started with Oboe](https://www.youtube.com/playlist?list=PLWz5rJ2EKKc_duWv9IPNvx9YBudNMmLSa)\n[Low Latency Audio - Because Your Ears Are Worth It](https://www.youtube.com/watch?v=8vOf_fDtur4)\n: Android Dev Summit 2018.\n\n[Winning on Android - How to optimize an Android audio app](https://www.youtube.com/watch?v=tWBojmBpS74)\n: Android Developer Challenge 2018.\n\n[High performance audio on Android](https://youtu.be/d3kfEeMZ65c)\n(Google I/O 2013)\n: The whole video is about latency.\n\n[Building great multi-media experiences on Android](https://youtu.be/92fgcUNCHic)\n(Google I/O 2014)\n: The first 14 minutes are about audio in general and input latency in particular.\n\n[Audio latency: buffer sizes](https://youtu.be/PnDK17zP9BI)\n(100 Days of Google Dev)\n: Describes the relationship between audio latency, buffer sizes, and task scheduling."]]