# AudioMixer

# AudioMixer

Artifact:[androidx.media3:media3-transformer](https://developer.android.com/jetpack/androidx/releases/media3)  
[View Source](https://cs.android.com/search?q=file:androidx/media3/transformer/AudioMixer.java+class:androidx.media3.transformer.AudioMixer)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/transformer/AudioMixer "View this page in Java")

<br />

```
@UnstableApi
interface AudioMixer
```

<br />

Known direct subclasses  
[DefaultAudioMixer](https://developer.android.com/reference/kotlin/androidx/media3/transformer/DefaultAudioMixer)  

|-------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [DefaultAudioMixer](https://developer.android.com/reference/kotlin/androidx/media3/transformer/DefaultAudioMixer) | An[AudioMixer](https://developer.android.com/reference/kotlin/androidx/media3/transformer/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](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)), or is unbounded if left unset.

**Updates:** The mixer supports the following updates at any time without the need for a[reset](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#reset()).

- [Add source](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)). Source audio will be included in future mixed output only.
- [Remove source](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#removeSource(int)).
- [Change source volume](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setSourceVolume(int,float)). The new volume will apply only to future source samples.
- [Change end time](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)). The new end time may cause an immediate change to the mixer[ended state](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#isEnded()).

[Changes](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat,int,long))to the output audio format, buffer size, or mixer start time require the mixer to first be[reset](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#reset()), discarding all buffered data.

**Operation:** The mixer must be[configured](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat,int,long))before any methods are called. Once configured, sources can queue audio data via[queueInput](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#queueInput(int,java.nio.ByteBuffer))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](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput())will return the mixed result. The cycle repeats until the mixer[isEnded](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#isEnded()).

## Summary

|                                                                                                                      ### Nested types                                                                                                                      |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `interface `[AudioMixer.Factory](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer.Factory) A factory for[AudioMixer](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer)instances. |

|                                ### Public functions                                 |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)         | [addSource](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long))`(sourceFormat: `[AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!, startTimeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Adds an audio source to mix starting at the given time.                                                                                |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [configure](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat,int,long))`(` ` outputAudioFormat: `[AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!,` ` bufferSizeMs: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` startTimeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) `)` Configures the mixer. |
| [ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!`   | [getOutput](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput())`()` Returns a buffer containing output audio data between its position and limit.                                                                                                                                                                                                                                                                                                                                                                         |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [hasSource](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#hasSource(int))`(sourceId: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns whether there is an[added source](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long))with the given`sourceId`.                                                                                                                           |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [isEnded](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#isEnded())`()` Returns whether the mixer can accept more[input](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#queueInput(int,java.nio.ByteBuffer))or produce more[output](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput()), based on the[end time](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)).                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [queueInput](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#queueInput(int,java.nio.ByteBuffer))`(sourceId: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, sourceBuffer: `[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!)` Queues audio data between the position and limit of the`sourceBuffer`.                                                                                                                                                               |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [removeSource](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#removeSource(int))`(sourceId: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Removes an audio source.                                                                                                                                                                                                                                                                                                                              |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [reset](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#reset())`()` Resets the mixer to its unconfigured state, releasing any resources.                                                                                                                                                                                                                                                                                                                                                                                          |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [setEndTimeUs](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setEndTimeUs(long))`(endTimeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Sets the end time of the output audio.                                                                                                                                                                                                                                                                                                            |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [setSourceVolume](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setSourceVolume(int,float))`(sourceId: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, volume: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Sets the volume applied to future samples queued from the given source.                                                                                                                                                                        |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [supportsSourceAudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#supportsSourceAudioFormat(androidx.media3.common.audio.AudioProcessor.AudioFormat))`(sourceFormat: `[AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!)` 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](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput())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](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!` | Audio format of source buffers.    |
| `startTimeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                            | Source start time in microseconds. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|-----------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Non-negative integer identifying the source (`sourceId`). |

|                                                                                                Throws                                                                                                 |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| [androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.UnhandledAudioFormatException) | If the source format is not supported. |

### 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](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#reset()).

The mixing buffer size is set by`bufferSizeMs`and indicates how much audio can be queued before[getOutput](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput())is called.  

|                                                                          Parameters                                                                          |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `outputAudioFormat: `[AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!` | The audio format of buffers returned from[getOutput](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput()).   |
| `bufferSizeMs: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                  | The optional mixing buffer size in milliseconds, or[LENGTH_UNSET](https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()). |
| `startTimeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                 | The start time of the mixer output in microseconds.                                                                                                        |

|                                                                                                Throws                                                                                                 |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|
| [androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.UnhandledAudioFormatException) | If the output audio format is not supported. |

### 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](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)). 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](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!` | A buffer containing output data between its position and limit. |

### hasSource

```
fun hasSource(sourceId: Int): Boolean
```

Returns whether there is an[added source](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long))with the given`sourceId`.  

### isEnded

```
fun isEnded(): Boolean
```

Returns whether the mixer can accept more[input](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#queueInput(int,java.nio.ByteBuffer))or produce more[output](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput()), based on the[end time](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)).

**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](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#getOutput())if all sources have queued data.  

|                                            Parameters                                             |
|---------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `sourceId: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)           | Source identifier from[addSource](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)).                                                                |
| `sourceBuffer: `[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!` | 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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Source identifier from[addSource](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)). |

### 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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The end time in microseconds. |

|                                                        Throws                                                         |
|-----------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [java.lang.IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | 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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | Source identifier from[addSource](https://developer.android.com/reference/kotlin/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)). |
| `volume: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | 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.