Skip to main content
The Android NDK provides high-performance audio APIs for applications that require low-latency audio processing, such as music apps, games, and real-time communication tools.

Audio APIs overview

Android offers two primary native audio APIs:
  • AAudio - Modern C API introduced in Android 8.0 (API level 26), designed for high-performance audio with minimal latency
  • OpenSL ES - Industry-standard API available since Android 2.3 (API level 9), provides broader device compatibility
For new applications targeting Android 8.0 and higher, use AAudio. It offers better performance and simpler API design.

Getting started with AAudio

AAudio provides a simple, callback-based approach to audio processing with automatic latency management.

Basic audio playback

1

Include the AAudio header

2

Create an audio stream

3

Implement the audio callback

4

Start and stop the stream

Achieving low latency

To minimize audio latency, follow these best practices:

Use exclusive mode

Exclusive mode gives your app direct access to the audio hardware, reducing latency at the cost of preventing other apps from playing audio simultaneously.

Set performance mode

Low-latency mode may increase power consumption. Use AAUDIO_PERFORMANCE_MODE_POWER_SAVING for background audio or when latency is not critical.

Query actual latency

Optimize callback processing

The audio callback runs on a high-priority thread. Follow these guidelines:
  • Keep processing minimal and deterministic
  • Avoid system calls, memory allocation, or locks
  • Don’t perform file I/O or network operations
  • Pre-allocate all buffers before starting the stream
  • Use lock-free data structures for sharing data with other threads
Blocking or taking too long in the audio callback will cause audio glitches (xruns).

OpenSL ES for legacy devices

For apps targeting devices running Android 7.1 (API level 25) or lower, use OpenSL ES.

Basic setup

Audio recording

To record audio with AAudio:
Don’t forget to request the RECORD_AUDIO permission in your app’s manifest and at runtime for Android 6.0 (API level 23) and higher.

Best practices

  • Test on real devices - Audio latency varies significantly across devices. Always test on target hardware.
  • Handle audio focus - Implement audio focus handling at the Java/Kotlin layer to respond appropriately when other apps need audio.
  • Monitor performance - Use AAudioStream_getXRunCount() to detect buffer underruns and overruns.
  • Provide fallback - Not all devices support low-latency audio. Gracefully degrade to shared mode if exclusive mode fails.
  • Use appropriate buffer sizes - Smaller buffers reduce latency but increase CPU usage and risk of glitches.

Additional resources