# AudioOutput

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/AudioOutput.java+class:androidx.media3.exoplayer.audio.AudioOutput)

*** ** * ** ***


```
public interface AudioOutput
```

<br />

Known direct subclasses [AudioTrackAudioOutput](https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput), [ForwardingAudioOutput](https://developer.android.com/reference/androidx/media3/exoplayer/audio/ForwardingAudioOutput)

|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioTrackAudioOutput` | 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`. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/audio/ForwardingAudioOutput` | A forwarding base class that delegates all calls to the provided `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |

*** ** * ** ***

An interface to wrap an object that can play audio, like an `https://developer.android.com/reference/android/media/AudioTrack.html`.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.Listener` Listener for `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput` events. |
| `public final class https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput.WriteException extends https://developer.android.com/reference/java/lang/Exception.html` Thrown when a failure occurs writing to the output. |

| ### Public methods |
|---|---|
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#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. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#attachAuxEffect(int)(int effectId)` Attaches an auxiliary effect to the output. |
| `default 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. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#flush()()` Flushes the audio data from the output. |
| `abstract int` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#getAudioSessionId()()` Returns the audio session ID or `https://developer.android.com/reference/androidx/media3/common/C#AUDIO_SESSION_ID_UNSET()` if unknown. |
| `abstract long` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#getBufferSizeInFrames()()` Returns the buffer size in frames of the underlying `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `abstract https://developer.android.com/reference/androidx/media3/common/PlaybackParameters` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#getPlaybackParameters()()` Returns the playback parameters. |
| `abstract long` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#getPositionUs()()` Returns the current playout timestamp in microseconds. |
| `abstract int` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#getSampleRate()()` Returns the sample rate of the underlying `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#isOffloadedPlayback()()` Returns whether this output is playing back in offload mode. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#isStalled()()` Returns whether the audio output is stalled. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#pause()()` Pauses playback. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#play()()` Starts or resumes playing audio. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#release()()` Releases the `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#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. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#setAuxEffectSendLevel(float)(float level)` Sets the send level for the auxiliary effect. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#setOffloadDelayPadding(int,int)(int delayInFrames, int paddingInFrames)` Sets the offload delay and padding. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#setOffloadEndOfStream()()` Notifies the output that this is the end of the stream. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#setPlaybackParameters(androidx.media3.common.PlaybackParameters)(https://developer.android.com/reference/androidx/media3/common/PlaybackParameters playbackParams)` Sets the playback parameters. |
| `default void` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#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. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#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. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#setVolume(float)(float volume)` Sets the volume of the `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#stop()()` Signal that the last buffer has been written and playback should stop after playing out all remaining buffers |
| `abstract boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/audio/AudioOutput#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. |

## Public methods

### addListener

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

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

### attachAuxEffect

```
abstract void attachAuxEffect(int effectId)
```

Attaches an auxiliary effect to the output.

### canReuseAudioOutput

```
@UnstableApi
default boolean canReuseAudioOutput(
    AudioOutputProvider.OutputConfig currentConfig,
    AudioOutputProvider.FormatConfig newFormat,
    AudioOutputProvider.OutputConfig newConfig
)
```

Returns if the configurations are sufficiently compatible to reuse this audio output.

### flush

```
abstract void flush()
```

Flushes the audio data from the output.

### getAudioSessionId

```
abstract int getAudioSessionId()
```

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

### getBufferSizeInFrames

```
abstract long getBufferSizeInFrames()
```

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

### getPlaybackParameters

```
abstract PlaybackParameters getPlaybackParameters()
```

Returns the playback parameters.

### getPositionUs

```
abstract long getPositionUs()
```

Returns the current playout timestamp in microseconds.

### getSampleRate

```
abstract int getSampleRate()
```

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

### isOffloadedPlayback

```
abstract boolean isOffloadedPlayback()
```

Returns whether this output is playing back in offload mode.

### isStalled

```
abstract boolean isStalled()
```

Returns whether the audio output is stalled.

### pause

```
abstract void pause()
```

Pauses playback.

### play

```
abstract void play()
```

Starts or resumes playing audio.

### release

```
abstract void release()
```

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

### removeListener

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

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

### setAuxEffectSendLevel

```
abstract void setAuxEffectSendLevel(float level)
```

Sets the send level for the auxiliary effect.

### setOffloadDelayPadding

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

Sets the offload delay and padding.

### setOffloadEndOfStream

```
abstract void setOffloadEndOfStream()
```

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

### setPlaybackParameters

```
abstract void setPlaybackParameters(PlaybackParameters playbackParams)
```

Sets the playback parameters.

### setPlayerId

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

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

### setPreferredDevice

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

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

### setVolume

```
abstract void setVolume(float volume)
```

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

### stop

```
abstract void stop()
```

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

### write

```
abstract 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. |