What is the difference between AudioTrack and AudioRecord?

Android provides various APIs for working with audio, allowing developers to play and record audio in their apps. The main audio APIs available include AudioTrack, which allows playback of raw audio data, and AudioRecord, which allows recording of raw audio data.
These low-level APIs provide a pathway to the audio hardware and allow precise control over audio playback and recording. However, they require developers to understand audio formats and manage buffers. Higher-level APIs like MediaPlayer are also available in Android for simpler audio playback use cases.
This article will provide an in-depth look at AudioTrack and AudioRecord, comparing their capabilities, use cases, performance, supported formats, and more. Code samples will demonstrate how to use these APIs in practice.
AudioTrack Overview
AudioTrack plays raw audio data and streams it to the audio hardware for playback (https://stackoverflow.com/questions/19417541/what-is-the-difference-between-various-sound-sources-in-android-system). It is used for streaming audio or playing short audio clips from memory like sound effects (https://dolby.io/blog/recording-audio-on-android-with-examples/). Some key points about AudioTrack:
- Used to play back raw PCM audio buffers.
- Streams audio data to audio hardware for playback.
- Can play short sound clips or continuous streaming audio.
- Useful for sound effects, music, beeps, etc.
- Lower latency than MediaPlayer for real-time audio.
In summary, AudioTrack takes raw audio buffers and outputs them to the audio hardware for real-time playback. It is commonly used for sound effects, music, or other short audio clips that fit in memory.
AudioRecord Overview
AudioRecord is used to record audio input from the mic or other audio sources on an Android device [1]. Some key features of AudioRecord include:
- Records raw audio data from the microphone or other audio input sources.
- Allows specifying parameters like sample rate, channel configuration, etc.
- Audio data can be read into a buffer in different formats like PCM 16 bit, PCM 8 bit, etc.
- Well suited for low latency audio input required by applications like voice chat, speech recognition, etc.
Overall, AudioRecord is focused on recording audio input streams efficiently in Android. It provides flexible options to configure the audio source and parameters.
Key Differences
The main difference between AudioTrack and AudioRecord is that AudioTrack is used for playing audio, while AudioRecord is used for recording audio.
AudioTrack provides a write-only interface that allows applications to write audio data that is then rendered and played by the audio framework.
Some key features of AudioTrack include:
- Plays raw PCM audio or compressed formats like MP3
- Audio can be streamed from memory or a file
- Volume and pan controls
- Playback position/time tracking
In contrast, AudioRecord provides a read-only interface to record audio from the device’s microphone or other audio input sources.
Key capabilities of AudioRecord include:
- Capturing raw PCM samples
- Reading data into a byte array
- Controlling sample rate, channel count, encoding
- Audio effects like noise suppression
In summary, AudioTrack handles audio playback while AudioRecord handles audio recording. Both provide low-level access and control over the audio framework.
AudioTrack Use Cases
AudioTrack is commonly used for playing back music or sound effects in Android apps. Some key use cases include:
Music Playback: AudioTrack allows streaming music from memory buffers, so it can be used to implement music players or streaming audio apps. The audio data can be fed to AudioTrack in a background thread while the main app UI thread remains responsive.
For example:
https://developer.android.com/reference/android/media/AudioTrack#playing-audio-from-a-byte-array shows how to play back audio data from a byte array using AudioTrack.
Sound Effects: AudioTrack is useful for sound effects that need to be synchronized to animations or game events. Since AudioTrack allows precise positioning of the write pointer, sound effects can be lined up accurately.
For instance:
https://stackoverflow.com/questions/2413426/playing-an-arbitrary-sound-effect-in-android demonstrates playing a short sound effect using AudioTrack when a button is clicked.
AudioRecord Use Cases
AudioRecord is commonly used for apps that need to record audio in real-time and process it in some way. Two of the main use cases are:
Voice Chat
AudioRecord allows recording audio from the device microphone and sending it across the network in real-time for voice chat applications. This is useful for apps like online gaming chat, live streaming, or video conferencing. AudioRecord provides low latency audio input compared to other recording options on Android. Some examples are Voice over IP (VoIP) and push-to-talk apps.
Speech Recognition
AudioRecord is the recommended class for streaming audio input to speech recognition engines like Google’s Cloud Speech-to-Text API. It allows recording the user’s voice and sending it to the API for transcription in real-time. This is useful for voice assistant apps, voice commands, or any app needing speech-to-text capabilities. AudioRecord optimizes the audio quality and sampling rate needed for accurate speech recognition.
Performance
AudioTrack and AudioRecord have some key performance differences in areas like latency and buffer sizes. As noted in this StackOverflow post, AudioRecord and AudioTrack latency, both classes can have high latency due to their minimum buffer size requirements. The buffer sizes help minimize glitches and dropouts in the audio playback or recording. However, the larger buffers mean increased latency between when audio is played/recorded and when it is actually heard.
AudioTrack has higher latency than AudioRecord in most cases. According to analysis on Medium, AudioTrack requires a larger minimum buffer size (12 Kb vs. 0.5 Kb for AudioRecord on some devices) to prevent glitching during playback. So AudioRecord will generally have lower latency for recording audio.
If ultra-low latency is needed, developers may need to use the lower level OpenSL ES API on Android instead of AudioTrack/AudioRecord. But for most use cases, the latency may be acceptable, as long as the buffer size is configured properly. Overall AudioRecord will provide lower latency for recording audio while AudioTrack offers reliable playback.
Audio Formats
The two main categories of audio file formats are uncompressed and compressed formats (Audio file format). The most common uncompressed format is PCM (Pulse Code Modulation), which stores raw digital audio samples. PCM audio is used for WAV and AIFF files. Compressed formats aim to reduce the file size through audio encoding while minimizing loss of quality. Popular compressed formats include MP3, AAC, Ogg Vorbis, FLAC, and WMA.
For Android audio development, some key factors to consider for audio formats are:
- PCM – Uncompressed so requires more storage space but preserves original quality.
- MP3 – Lossy compression optimized for music, widely supported format.
- AAC – More efficient than MP3, especially at low bitrates. Common for streaming.
- Ogg Vorbis – Open source compression format, produces small files.
- FLAC – Lossless compression, smaller than PCM with no quality loss.
The optimal audio format depends on the app’s requirements for sound quality, storage limitations, streaming use cases, and device compatibility (Can I Use).
Code Samples
Basic usage examples for AudioTrack in Android apps:
Initializing AudioTrack to play audio
Playing a WAV file with AudioTrack
Here is some sample Java code to initialize an AudioTrack to play a tone:
int sampleRate = 8000; int channelConfig = AudioFormat.CHANNEL_OUT_MONO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat); AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, bufferSize, AudioTrack.MODE_STREAM); audioTrack.play();
This creates a mono AudioTrack stream configured for 16-bit PCM audio at 8kHz sample rate. The buffer size is set to the minimum needed for these parameters.
Conclusion
In summary, AudioTrack and AudioRecord are two key APIs in Android for dealing with audio playback and recording. The main differences are:
- AudioTrack focuses on audio playback, while AudioRecord is for recording audio.
- AudioTrack streams audio data from RAM, while AudioRecord can store data to a file.
- AudioRecord allows more low-level control of audio input parameters.
In most cases, AudioTrack is the right choice for playback in apps where you have audio data already loaded in memory. AudioRecord is recommended for recording audio from the mic or other inputs. However, there are certain advanced use cases like streaming low latency audio where AudioRecord may be better suited even for playback.
Overall, both APIs provide robust tools for working with audio in Android apps. Understanding the key differences allows developers to choose the right one for their needs and build high quality audio experiences.