# AudioTrackAudioOutput

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

*** ** * ** ***


```
public final class AudioTrackAudioOutput implements AudioOutput
```

<br />

*** ** * ** ***

A default implementation of `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput` that wraps an `https://developer.android.com/reference/android/media/AudioTrack.html`.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi public interface https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.CapabilityChangeListener` Listener for potential capability change events. |
| `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi public final class https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.InvalidAudioTrackTimestampException extends https://developer.android.com/reference/java/lang/RuntimeException.html` Thrown when the audio track has provided a spurious timestamp, if `https://developer.android.com/reference/androidx/media3/common/MediaLibraryInfo#enableWorkarounds()` is false. |

| ### Public fields |
|---|---|
| `final https://developer.android.com/reference/android/media/AudioTrack.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#audioTrack()` |

| ### Public constructors |
|---|
| `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi [AudioTrackAudioOutput](https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#AudioTrackAudioOutput(android.media.AudioTrack,androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig,androidx.media3.exoplayer.audio.AudioTrackAudioOutput.CapabilityChangeListener,androidx.media3.common.util.Clock))( https://developer.android.com/reference/android/media/AudioTrack.html audioTrack, https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.OutputConfig config, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.CapabilityChangeListener capabilityChangeListener, https://developer.android.com/reference/androidx/media3/common/util/Clock clock )` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#AudioTrackAudioOutput(android.media.AudioTrack,androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig,androidx.media3.exoplayer.audio.AudioTrackAudioOutput.CapabilityChangeListener,float,androidx.media3.common.util.Clock)` instead. <br /> |
| `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#AudioTrackAudioOutput(android.media.AudioTrack,androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig,androidx.media3.exoplayer.audio.AudioTrackAudioOutput.CapabilityChangeListener,float,androidx.media3.common.util.Clock)( https://developer.android.com/reference/android/media/AudioTrack.html audioTrack, https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.OutputConfig config, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.CapabilityChangeListener capabilityChangeListener, float maxPlaybackSpeed, https://developer.android.com/reference/androidx/media3/common/util/Clock clock )` Creates a new instance. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#addListener(androidx.media3.exoplayer.audio.AudioOutput.Listener)(https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener listener)` Adds a `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener` for audio output events. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#attachAuxEffect(int)(int effectId)` Attaches an auxiliary effect to the output. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#flush()()` Flushes the audio data from the output. |
| `int` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#getAudioSessionId()()` Returns the audio session ID or `https://developer.android.com/reference/androidx/media3/common/C#AUDIO_SESSION_ID_UNSET()` if unknown. |
| `https://developer.android.com/reference/android/media/AudioTrack.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#getAudioTrack()()` Returns the `https://developer.android.com/reference/android/media/AudioTrack.html` instance used for audio output. |
| `long` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#getBufferSizeInFrames()()` Returns the buffer size in frames of the underlying `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `https://developer.android.com/reference/androidx/media3/common/PlaybackParameters` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#getPlaybackParameters()()` Returns the playback parameters. |
| `long` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#getPositionUs()()` Returns the current playout timestamp in microseconds. |
| `int` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#getSampleRate()()` Returns the sample rate of the underlying `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#isOffloadedPlayback()()` Returns whether this output is playing back in offload mode. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#isStalled()()` Returns whether the audio output is stalled. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#pause()()` Pauses playback. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#play()()` Starts or resumes playing audio. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#release()()` Releases the `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#removeListener(androidx.media3.exoplayer.audio.AudioOutput.Listener)(https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener listener)` Removes a `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener` for audio output events. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setAuxEffectSendLevel(float)(float level)` Sets the send level for the auxiliary effect. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setOffloadDelayPadding(int,int)(int delayInFrames, int paddingInFrames)` Sets the offload delay and padding. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setOffloadEndOfStream()()` Notifies the output that this is the end of the stream. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setPlaybackParameters(androidx.media3.common.PlaybackParameters)(https://developer.android.com/reference/androidx/media3/common/PlaybackParameters playbackParameters)` Sets the playback parameters. |
| `void` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setPlayerId(androidx.media3.exoplayer.analytics.PlayerId)(https://developer.android.com/reference/androidx/media3/exoplayer/analytics/PlayerId playerId)` Sets the `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/PlayerId` on the audio output. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setPreferredDevice(android.media.AudioDeviceInfo)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/media/AudioDeviceInfo.html preferredDevice)` Sets the preferred audio device for routing, or null for no preference. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#setVolume(float)(float volume)` Sets the volume of the `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#stop()()` Signal that the last buffer has been written and playback should stop after playing out all remaining buffers |
| `boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#write(java.nio.ByteBuffer,int,long)( https://developer.android.com/reference/java/nio/ByteBuffer.html buffer, int encodedAccessUnitCount, long presentationTimeUs )` Writes audio data to the audio output for playback with a presentation timestamp. |

| ### Inherited methods |
|---|
| From [androidx.media3.exoplayer.audio.AudioOutput](https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput) |---|---| | `boolean` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#canReuseAudioOutput(androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig,androidx.media3.exoplayer.audio.AudioOutputProvider.FormatConfig,androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig)( https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.OutputConfig currentConfig, https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.FormatConfig newFormat, https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.OutputConfig newConfig )` Returns if the configurations are sufficiently compatible to reuse this audio output. | |

## Public fields

### audioTrack

```
public final AudioTrack audioTrack
```

## Public constructors

### AudioTrackAudioOutput

```
@UnstableApi
public [AudioTrackAudioOutput](https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#AudioTrackAudioOutput(android.media.AudioTrack,androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig,androidx.media3.exoplayer.audio.AudioTrackAudioOutput.CapabilityChangeListener,androidx.media3.common.util.Clock))(
    AudioTrack audioTrack,
    AudioOutputProvider.OutputConfig config,
    @Nullable AudioTrackAudioOutput.CapabilityChangeListener capabilityChangeListener,
    Clock clock
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput#AudioTrackAudioOutput(android.media.AudioTrack,androidx.media3.exoplayer.audio.AudioOutputProvider.OutputConfig,androidx.media3.exoplayer.audio.AudioTrackAudioOutput.CapabilityChangeListener,float,androidx.media3.common.util.Clock)` instead.

### AudioTrackAudioOutput

```
@UnstableApi
public AudioTrackAudioOutput(
    AudioTrack audioTrack,
    AudioOutputProvider.OutputConfig config,
    @Nullable AudioTrackAudioOutput.CapabilityChangeListener capabilityChangeListener,
    float maxPlaybackSpeed,
    Clock clock
)
```

Creates a new instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/AudioTrack.html audioTrack` | The audio track to wrap. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.OutputConfig config` | The output configuration. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.CapabilityChangeListener capabilityChangeListener` | The `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput.CapabilityChangeListener`. |
| `float maxPlaybackSpeed` | The maximum playback speed set on the track if `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutputProvider.OutputConfig#usePlaybackParameters()` is enabled. |
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The `https://developer.android.com/reference/androidx/media3/common/util/Clock`. |

## Public methods

### addListener

```
public void addListener(AudioOutput.Listener listener)
```

Adds a `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener` for audio output events.

### attachAuxEffect

```
public void attachAuxEffect(int effectId)
```

Attaches an auxiliary effect to the output.

### flush

```
public void flush()
```

Flushes the audio data from the output.

### getAudioSessionId

```
public int getAudioSessionId()
```

Returns the audio session ID or `https://developer.android.com/reference/androidx/media3/common/C#AUDIO_SESSION_ID_UNSET()` if unknown.

### getAudioTrack

```
public AudioTrack getAudioTrack()
```

Returns the `https://developer.android.com/reference/android/media/AudioTrack.html` instance used for audio output.

### getBufferSizeInFrames

```
public long getBufferSizeInFrames()
```

Returns the buffer size in frames of the underlying `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`.

### getPlaybackParameters

```
public PlaybackParameters getPlaybackParameters()
```

Returns the playback parameters.

### getPositionUs

```
public long getPositionUs()
```

Returns the current playout timestamp in microseconds.

### getSampleRate

```
public int getSampleRate()
```

Returns the sample rate of the underlying `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`.

### isOffloadedPlayback

```
public boolean isOffloadedPlayback()
```

Returns whether this output is playing back in offload mode.

### isStalled

```
public boolean isStalled()
```

Returns whether the audio output is stalled.

### pause

```
public void pause()
```

Pauses playback.

### play

```
public void play()
```

Starts or resumes playing audio.

### release

```
public void release()
```

Releases the `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`.

### removeListener

```
public void removeListener(AudioOutput.Listener listener)
```

Removes a `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener` for audio output events.

### setAuxEffectSendLevel

```
public void setAuxEffectSendLevel(float level)
```

Sets the send level for the auxiliary effect.

### setOffloadDelayPadding

```
public void setOffloadDelayPadding(int delayInFrames, int paddingInFrames)
```

Sets the offload delay and padding.

### setOffloadEndOfStream

```
public void setOffloadEndOfStream()
```

Notifies the output that this is the end of the stream.

### setPlaybackParameters

```
public void setPlaybackParameters(PlaybackParameters playbackParameters)
```

Sets the playback parameters.

### setPlayerId

```
@UnstableApi
public void setPlayerId(PlayerId playerId)
```

Sets the `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/PlayerId` on the audio output.

### setPreferredDevice

```
public void setPreferredDevice(@Nullable AudioDeviceInfo preferredDevice)
```

Sets the preferred audio device for routing, or null for no preference.

### setVolume

```
public void setVolume(float volume)
```

Sets the volume of the `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`.

### stop

```
public void stop()
```

Signal that the last buffer has been written and playback should stop after playing out all remaining buffers

### write

```
public boolean write(
    ByteBuffer buffer,
    int encodedAccessUnitCount,
    long presentationTimeUs
)
```

Writes audio data to the audio output for playback with a presentation timestamp.

The provided `buffer` must be a direct byte buffer with native byte order. The method is read-only and will not modify the bytes. It processes data from `buffer.position()` to `buffer.limit()` (exclusive). Every call will advance `buffer.position()` to reflect the number of bytes written.

The first time this method is called with a new buffer, the buffer must contain one or more complete encoded access units or complete PCM frames.

If this method returns `false`, the same buffer must be used for subsequent calls to this method until it has been fully consumed, or until an intervening call to `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#flush()`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer.html buffer` | The buffer containing the audio data. |
| `int encodedAccessUnitCount` | The number of encoded access units in the buffer when the buffer was first used in this method, or 1 if the buffer contains PCM audio. When calling this method again after it returned `false`, the same number of encoded access units must be provided. |
| `long presentationTimeUs` | The presentation timestamp, in microseconds, of the first access unit or PCM frame when the buffer was first used in this method. When calling this method again after it returned `false`, the same timestamp must be provided. |

| Returns |
|---|---|
| `boolean` | Whether the buffer was handled fully. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.WriteException` | If the write fails. The audio output cannot be reused after a failure and needs to be recreated. |