自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
音频延迟(适用于应用开发者)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
为了尽可能缩短音频延迟,我们建议您使用 Oboe。
Oboe
Oboe 使用原生音频,基于 AAudio(对于 Android 8.1 及更高版本)和 OpenSL ES(对于较低版本的 Android)构建而成。为了尽可能缩短设备的延迟时间,请设置 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 中的 bug
如果您在使用 Oboe 时遇到问题,可以提交 bug。
视频
- Oboe 入门指南
- 低延迟音频 - 您的耳朵需要保护
- 2018 年 Android 开发者峰会。
- 在 Android 上取得成功 - 如何优化 Android 音频应用
- 2028 年 Android 开发者挑战赛。
- Android 上的高性能音频(2013 年 Google I/O 大会)
- 整个视频都是关于音频延迟的。
- 在 Android 上打造出色的多媒体体验(2014 年 Google I/O 大会)
- 前 14 分钟大致介绍了一下音频,尤其是输入延迟时间。
- 音频延迟:缓冲区空间 (100 Days of Google Dev)
- 介绍音频延迟、缓冲区空间和任务调度之间的关系。
本页面上的内容和代码示例受内容许可部分所述许可的限制。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 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."]]