@UnstableApi
interface AudioMixer

Known direct subclasses
DefaultAudioMixer

An AudioMixer that incrementally mixes source audio into a fixed size mixing buffer.


An audio component which combines audio data from multiple sources into a single output.

The mixer supports an arbitrary number of concurrent sources and will ensure audio data from all sources are aligned and mixed before producing output. Any periods without sources will be filled with silence. The total duration of the mixed track is controlled with setEndTimeUs, or is unbounded if left unset.

Updates: The mixer supports the following updates at any time without the need for a reset.

Changes to the output audio format, buffer size, or mixer start time require the mixer to first be reset, discarding all buffered data.

Operation: The mixer must be configured before any methods are called. Once configured, sources can queue audio data via queueInput and the mixer will consume input audio up to the configured buffer size and end time. Once all sources have produced data for a period then getOutput will return the mixed result. The cycle repeats until the mixer isEnded.

Summary

Nested types

A factory for AudioMixer instances.

Public functions

Int
addSource(sourceFormat: AudioProcessor.AudioFormat!, startTimeUs: Long)

Adds an audio source to mix starting at the given time.

Unit
configure(
    outputAudioFormat: AudioProcessor.AudioFormat!,
    bufferSizeMs: Int,
    startTimeUs: Long
)

Configures the mixer.

java-static AudioMixer!

This function is deprecated.

Use create.

ByteBuffer!

Returns a buffer containing output audio data between its position and limit.

Boolean
hasSource(sourceId: Int)

Returns whether there is an added source with the given sourceId.

Boolean

Returns whether the mixer can accept more input or produce more output, based on the end time.

Unit
queueInput(sourceId: Int, sourceBuffer: ByteBuffer!)

Queues audio data between the position and limit of the sourceBuffer.

Unit
removeSource(sourceId: Int)

Removes an audio source.

Unit

Resets the mixer to its unconfigured state, releasing any resources.

Unit
setEndTimeUs(endTimeUs: Long)

Sets the end time of the output audio.

Unit
setSourceVolume(sourceId: Int, volume: Float)

Sets the volume applied to future samples queued from the given source.

Boolean

Indicates whether the mixer supports mixing sources with the given audio format.

Public functions

addSource

fun addSource(sourceFormat: AudioProcessor.AudioFormat!, startTimeUs: Long): Int

Adds an audio source to mix starting at the given time.

If the mixer has already output samples past the startTimeUs, audio from this source will be discarded up to the last output end timestamp.

If the source start time is earlier than the configured mixer start time then audio from this source will be discarded up to the mixer start time.

All audio sources start with a volume of 1.0 on all channels.

Parameters
sourceFormat: AudioProcessor.AudioFormat!

Audio format of source buffers.

startTimeUs: Long

Source start time in microseconds.

Returns
Int

Non-negative integer identifying the source (sourceId).

configure

fun configure(
    outputAudioFormat: AudioProcessor.AudioFormat!,
    bufferSizeMs: Int,
    startTimeUs: Long
): Unit

Configures the mixer.

The mixer must be configured before use and can only be reconfigured after a call to reset.

The mixing buffer size is set by bufferSizeMs and indicates how much audio can be queued before getOutput is called.

Parameters
outputAudioFormat: AudioProcessor.AudioFormat!

The audio format of buffers returned from getOutput.

bufferSizeMs: Int

The optional mixing buffer size in milliseconds, or LENGTH_UNSET.

startTimeUs: Long

The start time of the mixer output in microseconds.

Throws
androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException

If the output audio format is not supported.

create

java-static fun create(): AudioMixer!

getOutput

fun getOutput(): ByteBuffer!

Returns a buffer containing output audio data between its position and limit.

The buffer will be no larger than the configured buffer size and will include no more than the frames that have been queued from all sources, up to the end time. Silence will be generated for any periods with no sources.

The buffer will always be a direct byte buffer with native byte order. Calling this method invalidates any previously returned buffer. The buffer will be empty if no output is available.

Returns
ByteBuffer!

A buffer containing output data between its position and limit.

hasSource

fun hasSource(sourceId: Int): Boolean

Returns whether there is an added source with the given sourceId.

isEnded

fun isEnded(): Boolean

Returns whether the mixer can accept more input or produce more output, based on the end time.

Note: If no end time is set this will always return false.

queueInput

fun queueInput(sourceId: Int, sourceBuffer: ByteBuffer!): Unit

Queues audio data between the position and limit of the sourceBuffer.

After calling this method output may be available via getOutput if all sources have queued data.

Parameters
sourceId: Int

Source identifier from addSource.

sourceBuffer: ByteBuffer!

The source buffer to mix. It must be a direct byte buffer with native byte order. Its contents are treated as read-only. Its position will be advanced by the number of bytes consumed (which may be zero). The caller retains ownership of the provided buffer.

removeSource

fun removeSource(sourceId: Int): Unit

Removes an audio source.

No more audio can be queued from this source. All audio queued before removal will be output.

Parameters
sourceId: Int

Source identifier from addSource.

reset

fun reset(): Unit

Resets the mixer to its unconfigured state, releasing any resources.

setEndTimeUs

fun setEndTimeUs(endTimeUs: Long): Unit

Sets the end time of the output audio.

The mixer will not accept input nor produce output past this point.

Parameters
endTimeUs: Long

The end time in microseconds.

Throws
java.lang.IllegalArgumentException

If endTimeUs is before the configured start time.

setSourceVolume

fun setSourceVolume(sourceId: Int, volume: Float): Unit

Sets the volume applied to future samples queued from the given source.

Parameters
sourceId: Int

Source identifier from addSource.

volume: Float

Non-negative scalar applied to all source channels.

supportsSourceAudioFormat

fun supportsSourceAudioFormat(sourceFormat: AudioProcessor.AudioFormat!): Boolean

Indicates whether the mixer supports mixing sources with the given audio format.