Audio Loopback: How Does It Work in Android

What is Audio Loopback?

Audio loopback refers to the routing of audio internally within a computer system or device [1]. It allows audio from one application to be sent to another application as an input. For example, audio loopback enables the audio output from a media player to be routed back into a recording software as an input source. This loopback pathway creates an internal “virtual” audio cable between applications.

With audio loopback, the sound from any playback app on a device can be captured and recorded without external cables. It provides more flexibility compared to simply recording the default desktop/system audio output. Apps that generate audio, like games and web browsers, can have their audio fed into recording, streaming, or chat software via loopback. Overall, it allows more control over internal audio routing [2].

Why Use Audio Loopback?

Audio loopback has a couple key uses when it comes to audio recording and streaming:

One is for monitoring audio during recording sessions. Loopback allows you to hear audio sources in real-time as you’re recording them. This is useful for monitoring microphones, instruments, game audio, or any other input while it’s being captured. Loopback routes the audio back to your headphones or speakers so you can listen as you record (Audient).

Another common use is for creating a mix minus to avoid audio feedback. If you’re live streaming a podcast, for example, loopback lets you route microphone audio into the stream while excluding the stream audio from your headphones. This prevents echo or feedback since the microphone won’t pick up audio playback. Loopback gives fine-grained control over routing to create these mix minuses (Sweetwater).

Audio Loopback in Android

Android has native support for audio loopback, which allows audio playback to be routed back into the audio input. This enables capturing the system audio output for recording or live streaming purposes.

To enable audio loopback in Android, a few requirements need to be met:

  • The device must be running Android 4.1 Jelly Bean or later, as this is when the loopback feature was introduced.
  • The application needs to request the RECORD_AUDIO permission in order to capture audio.
  • The application must configure the audio session to enable loopback by setting setSessionId(SESSION_ID_LOOPBACK) before initializing the audio recorder.

By meeting these requirements, the audio session will automatically route output audio to the input stream so it can be recorded or broadcasted. The audio output device such as speakers or headphones must also be enabled.

According to the Android documentation, the loopback audio path has about 200ms of additional latency compared to the normal recording path. So this needs to be accounted for when measuring round-trip audio latency or synchronizing loopback audio.

Enabling Loopback in Android Studio

To enable audio loopback in an Android app, the first step is to add the loopback permission in the Android manifest file. This is done by adding the following line inside the tags:

<uses-permission android:name="android.permission.LOOP_RECORD_AUDIO" />

This allows the app to capture all audio played on the device, not just from the app itself. Without this permission, loopback will not work.

Next, to actually capture the audio, the AudioRecord API needs to be used. Here is sample code to initialize an AudioRecord instance:


AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.REMOTE_SUBMIX,
sampleRate, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);

The key parameters are setting the audio source to REMOTE_SUBMIX, which captures the mixed audio output, and specifying the sample rate, channels, encoding and buffer size as needed.

Once initialized, the AudioRecord instance can start recording the loopback audio which can then be processed or played back as needed in the app. Properly configuring the manifest permissions and using the AudioRecord API are the core requirements for enabling audio loopback on Android.

Routing Audio with Loopback

To route audio with loopback in Android, you first need to understand audio sources and sinks. Audio sources generate audio, like the microphone or an audio track in an app. Audio sinks consume audio, like the speaker or headphones.

By default, Android routes audio from sources directly to sinks. For example, microphone audio goes to the speaker. With loopback, you can intercept this audio flow and route it elsewhere.

To enable loopback, your app acts as both a sink and source. Your app registers itself as an audio sink, so it can receive audio from sources like the mic. It then re-transmits that audio as a source to another sink, like the speaker. This allows you to capture audio, process it, and loop it back out.

The key is configuring your app properly to act as both a sink and source. Android provides AudioRecord and AudioTrack classes to help manage this loopback routing. You register your app as a sink with AudioRecord, then send the audio to AudioTrack which acts as the source.

With the right setup, you can seamlessly loop audio between any app and system sources and sinks. This enables powerful audio processing and routing capabilities.

Loopback Latency

Loopback latency refers to the delay between when an audio signal is generated and when it is received back through the device’s audio input after being routed out the audio output. This round-trip delay is caused by the processing time required for the audio to travel through the various components in the device.

There are a few key factors that contribute to loopback latency on Android devices:

  • Audio buffer sizes – Larger audio buffers lead to more latency as more audio needs to be prepared before playback.
  • Audio hardware and drivers – Low quality audio components or poorly optimized drivers can increase latency.
  • System workload – Heavier system load causes increased scheduling delays.

To reduce loopback latency, it’s recommended to use smaller audio buffer sizes, enable audio fast paths in the OS, and minimize background tasks. Low latency audio capabilities were also introduced in Android 8.0 Oreo to allow high priority audio processing. For testing purposes, loopback latency under 5-6ms is generally desirable for real-time monitoring.

Loopback for Recording

One of the most common uses for audio loopback on Android is to record internal audio output. This allows you to capture system sounds, music, videos, game audio, and more that is playing internally on your device. To record looped audio, you need to enable loopback routing and setup an audio recorder to capture the looped stream.

The main ways to record internal Android audio with loopback are:

  • Use the Android AudioRecord API to capture the raw looped PCM audio stream. You can then encode this stream to a format like MP3 or AAC and save it to a file using MediaRecorder or other audio encoding libraries.
  • Route looped audio through an app like Audacity, which can directly record the looped stream to a file. Audacity needs loopback enabled but can handle the recording and encoding.
  • On a rooted device, use apps like Mobizen Screen Recorder that can directly access the internal audio buffer and save it.

The key advantages of loopback for recording are that it captures the raw internal audio before it’s output, so you get a clean, high-quality source. This allows recording even DRM-protected streams. You can record long durations hands-free. And loopback recording works consistently across Android models compared to other internal recording methods.[1]

The main challenges with loopback recording are dealing with latency, routing the audio streams properly, and encoding the audio into a standard format. But overall it provides one of the most flexible ways to reliably capture internal Android audio.

Loopback for Live Streaming

Using loopback for live streaming allows you to send audio from your Android device to your streaming software. This makes it easy to stream audio that is playing locally on your phone.

One benefit of using loopback for streaming is that you can apply audio effects within your Android app before sending the audio to the streaming software. For example, you could add reverb or other sound effects to your looped audio tracks before streaming them.

To use loopback for streaming:

  • Set up loopback routing in your Android app to send audio to the virtual input.
  • Open your streaming software and select the virtual input as the audio source.
  • Start playback of your audio tracks in the Android app and they will be streamed.
  • Apply any desired audio effects within your Android app before the loopback.

The looped audio will be sent to the streaming software in real-time, allowing you to stream audio tracks with effects from your Android device. Just make sure to adjust audio levels appropriately to avoid peaking or clipping.

Troubleshooting Loopback

Loopback can be a useful feature for routing audio in Android, but you may encounter some issues getting it set up properly. Here are some troubleshooting tips for common loopback problems.

Debugging No Audio Issues

If you are not hearing any audio when using loopback, there are a few things to check:

  • Make sure loopback is enabled in your app or Android Studio as described above.
  • Check that your app has microphone permissions granted.
  • Try increasing the microphone gain/volume in your Android device’s settings.
  • If using a USB microphone or headset, check it is properly connected and selected as the input device.
  • Test recording regular audio without loopback to isolate the issue.

Fixing Echoes

Hearing echoes during loopback is a common problem. There are a couple ways to address this:

  • Enable audio processing options like acoustic echo cancellation and noise suppression in your app code or Android Studio.
  • Reduce the loopback volume level to minimize echo feedback.
  • Use headphones instead of speakers to avoid picking up output audio.
  • Increase distance between your microphone and speakers.
  • Try an external sound card with better echo cancellation.

With some trial and error, you should be able to resolve any looping or echo issues and get high quality audio routing via loopback.

Alternatives to Loopback

While Loopback is a popular audio routing app for macOS, there are some alternatives worth considering for Android devices:

Virtual audio cables like VB-Audio VoiceMeeter allow you to route audio between applications on your device. They work by installing virtual audio devices that you can select as inputs and outputs in your apps. VoiceMeeter offers similar functionality to Loopback but works on Windows instead of macOS.

Dedicated external hardware mixers can also facilitate audio routing without any software required. Devices like the Behringer Xenyx mixer allow you to connect multiple audio sources, mix them together, and route them to outputs. This gives you flexibility in routing audio without relying on virtual software cables.

While Loopback is limited to macOS, alternatives like virtual audio cables and hardware mixers offer similar audio routing capabilities for Android and Windows users. The best option depends on your specific needs and setup.

Leave a Reply

Your email address will not be published. Required fields are marked *