# MediaCodecAdapter

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

*** ** * ** ***


```
@UnstableApi
public interface MediaCodecAdapter
```

<br />

Known direct subclasses [ForwardingMediaCodecAdapter](https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/ForwardingMediaCodecAdapter), [SynchronousMediaCodecAdapter](https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/SynchronousMediaCodecAdapter)

|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/ForwardingMediaCodecAdapter` | A `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter` instance that forwards all calls to its delegate. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/SynchronousMediaCodecAdapter` | A `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter` that operates the underlying `https://developer.android.com/reference/android/media/MediaCodec.html` in synchronous mode. |

*** ** * ** ***

Abstracts `https://developer.android.com/reference/android/media/MediaCodec.html` operations.

`MediaCodecAdapter` offers a common interface to interact with a `https://developer.android.com/reference/android/media/MediaCodec.html` regardless of the mode the `https://developer.android.com/reference/android/media/MediaCodec.html` is operating in.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.Configuration` Configuration parameters for a `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter`. |
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.Factory` A factory for `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter` instances. |
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.OnBufferAvailableListener` Listener to be called when an input or output buffer becomes available. |
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.OnFrameRenderedListener` Listener to be called when an output frame has rendered on the output surface. |

| ### Public methods |
|---|---|
| `abstract int` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#dequeueInputBufferIndex()()` Returns the next available input buffer index from the underlying `https://developer.android.com/reference/android/media/MediaCodec.html` or `https://developer.android.com/reference/android/media/MediaCodec.html#INFO_TRY_AGAIN_LATER` if no such buffer exists. |
| `abstract int` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#dequeueOutputBufferIndex(android.media.MediaCodec.BufferInfo)(https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html bufferInfo)` Returns the next available output buffer index from the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 35) https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#detachOutputSurface()()` Detaches the current output surface. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#flush()()` Flushes the adapter and the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `abstract @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/nio/ByteBuffer.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getInputBuffer(int)(int index)` Returns a writable ByteBuffer object for a dequeued input buffer index. |
| `abstract https://developer.android.com/reference/android/os/PersistableBundle.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getMetrics()()` Returns metrics data about the current codec instance. |
| `abstract @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/nio/ByteBuffer.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputBuffer(int)(int index)` Returns a read-only ByteBuffer for a dequeued output buffer index. |
| `abstract https://developer.android.com/reference/android/media/MediaFormat.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputFormat()()` Gets the `https://developer.android.com/reference/android/media/MediaFormat.html` that was output from the `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#needsReconfiguration()()` Whether the adapter needs to be reconfigured before it is used. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#queueInputBuffer(int,int,int,long,int)( int index, int offset, int size, long presentationTimeUs, int flags )` Submit an input buffer for decoding. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#queueSecureInputBuffer(int,int,androidx.media3.decoder.CryptoInfo,long,int)( int index, int offset, https://developer.android.com/reference/androidx/media3/decoder/CryptoInfo info, long presentationTimeUs, int flags )` Submit an input buffer that is potentially encrypted for decoding. |
| `default boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#registerOnBufferAvailableListener(androidx.media3.exoplayer.mediacodec.MediaCodecAdapter.OnBufferAvailableListener)( https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.OnBufferAvailableListener listener )` Registers a listener that will be called when an input or output buffer becomes available. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#release()()` Releases the adapter and the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#releaseOutputBuffer(int,boolean)(int index, boolean render)` Returns the buffer to the `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#releaseOutputBuffer(int,long)(int index, long renderTimeStampNs)` Updates the output buffer's surface timestamp and sends it to the `https://developer.android.com/reference/android/media/MediaCodec.html` to render it on the output surface. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#setOnFrameRenderedListener(androidx.media3.exoplayer.mediacodec.MediaCodecAdapter.OnFrameRenderedListener,android.os.Handler)( https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter.OnFrameRenderedListener listener, https://developer.android.com/reference/android/os/Handler.html handler )` Registers a callback to be invoked when an output frame is rendered on the output surface. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#setOutputSurface(android.view.Surface)(https://developer.android.com/reference/android/view/Surface.html surface)` Dynamically sets the output surface of a `https://developer.android.com/reference/android/media/MediaCodec.html`. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#setParameters(android.os.Bundle)(https://developer.android.com/reference/android/os/Bundle.html params)` Communicate additional parameter changes to the `https://developer.android.com/reference/android/media/MediaCodec.html` instance. |
| `abstract void` | `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#setVideoScalingMode(int)(@https://developer.android.com/reference/androidx/media3/common/C.VideoScalingMode int scalingMode)` Specifies the scaling mode to use, if a surface was specified when the codec was created. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 31) https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#subscribeToVendorParameters(java.util.List<java.lang.String>)(https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/java/lang/String.html> names)` Subscribe to vendor parameters, so that these parameters will be present in `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputFormat()` and changes to these parameters generate output format change event. |
| `abstract void` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 31) https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#unsubscribeFromVendorParameters(java.util.List<java.lang.String>)(https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/java/lang/String.html> names)` Unsubscribe from vendor parameters, so that these parameters will not be present in `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputFormat()` and changes to these parameters no longer generate output format change event. |
| `default void` | `<E extends https://developer.android.com/reference/java/lang/Exception.html> https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#useBuffer(androidx.media3.common.util.ThrowingRunnable<E>)(https://developer.android.com/reference/androidx/media3/common/util/ThrowingRunnable<E> runnable)` Uses an acquired input or output buffer (e.g. to read from or write data to it). |

## Public methods

### dequeueInputBufferIndex

```
abstract int dequeueInputBufferIndex()
```

Returns the next available input buffer index from the underlying `https://developer.android.com/reference/android/media/MediaCodec.html` or `https://developer.android.com/reference/android/media/MediaCodec.html#INFO_TRY_AGAIN_LATER` if no such buffer exists.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the underlying `https://developer.android.com/reference/android/media/MediaCodec.html` raised an error. |

### dequeueOutputBufferIndex

```
abstract int dequeueOutputBufferIndex(MediaCodec.BufferInfo bufferInfo)
```

Returns the next available output buffer index from the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`. If the next available output is a MediaFormat change, it will return `https://developer.android.com/reference/android/media/MediaCodec.html#INFO_OUTPUT_FORMAT_CHANGED` and you should call `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputFormat()` to get the format. If there is no available output, this method will return `https://developer.android.com/reference/android/media/MediaCodec.html#INFO_TRY_AGAIN_LATER`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the underlying `https://developer.android.com/reference/android/media/MediaCodec.html` raised an error. |

### detachOutputSurface

```
@RequiresApi(value = 35)
abstract void detachOutputSurface()
```

Detaches the current output surface.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#detachOutputSurface()` |   |

### flush

```
abstract void flush()
```

Flushes the adapter and the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`.

### getInputBuffer

```
abstract @Nullable ByteBuffer getInputBuffer(int index)
```

Returns a writable ByteBuffer object for a dequeued input buffer index.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#getInputBuffer(int)` |   |

### getMetrics

```
@RequiresApi(value = 26)
abstract PersistableBundle getMetrics()
```

Returns metrics data about the current codec instance.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#getMetrics()` |   |

### getOutputBuffer

```
abstract @Nullable ByteBuffer getOutputBuffer(int index)
```

Returns a read-only ByteBuffer for a dequeued output buffer index.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#getOutputBuffer(int)` |   |

### getOutputFormat

```
abstract MediaFormat getOutputFormat()
```

Gets the `https://developer.android.com/reference/android/media/MediaFormat.html` that was output from the `https://developer.android.com/reference/android/media/MediaCodec.html`.

Call this method if a previous call to `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#dequeueOutputBufferIndex(android.media.MediaCodec.BufferInfo)` returned `https://developer.android.com/reference/android/media/MediaCodec.html#INFO_OUTPUT_FORMAT_CHANGED`.

### needsReconfiguration

```
abstract boolean needsReconfiguration()
```

Whether the adapter needs to be reconfigured before it is used.

### queueInputBuffer

```
abstract void queueInputBuffer(
    int index,
    int offset,
    int size,
    long presentationTimeUs,
    int flags
)
```

Submit an input buffer for decoding.

The `index` must be an input buffer index that has been obtained from a previous call to `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#dequeueInputBufferIndex()`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#queueInputBuffer(int, int, int, long, int)` |   |

### queueSecureInputBuffer

```
abstract void queueSecureInputBuffer(
    int index,
    int offset,
    CryptoInfo info,
    long presentationTimeUs,
    int flags
)
```

Submit an input buffer that is potentially encrypted for decoding.

The `index` must be an input buffer index that has been obtained from a previous call to `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#dequeueInputBufferIndex()`.

This method behaves like `https://developer.android.com/reference/android/media/MediaCodec.html#queueSecureInputBuffer(int, int, android.media.MediaCodec.CryptoInfo, long, int)`, with the difference that `info` is of type `https://developer.android.com/reference/androidx/media3/decoder/CryptoInfo` and not `https://developer.android.com/reference/android/media/MediaCodec.CryptoInfo.html`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#queueSecureInputBuffer(int, int, android.media.MediaCodec.CryptoInfo, long, int)` |   |

### registerOnBufferAvailableListener

```
default boolean registerOnBufferAvailableListener(
    MediaCodecAdapter.OnBufferAvailableListener listener
)
```

Registers a listener that will be called when an input or output buffer becomes available.

Returns false if listener was not successfully registered for callbacks.

| Returns |
|---|---|
| `boolean` | Whether listener was successfully registered. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.Callback.html#onInputBufferAvailable(android.media.MediaCodec, int)` |   |
| `https://developer.android.com/reference/android/media/MediaCodec.Callback.html#onOutputBufferAvailable(android.media.MediaCodec, int, android.media.MediaCodec.BufferInfo)` |   |

### release

```
abstract void release()
```

Releases the adapter and the underlying `https://developer.android.com/reference/android/media/MediaCodec.html`.

### releaseOutputBuffer

```
abstract void releaseOutputBuffer(int index, boolean render)
```

Returns the buffer to the `https://developer.android.com/reference/android/media/MediaCodec.html`. If the `https://developer.android.com/reference/android/media/MediaCodec.html` was configured with an output surface, setting `render` to `true` will first send the buffer to the output surface. The surface will release the buffer back to the codec once it is no longer used/displayed.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#releaseOutputBuffer(int, boolean)` |   |

### releaseOutputBuffer

```
abstract void releaseOutputBuffer(int index, long renderTimeStampNs)
```

Updates the output buffer's surface timestamp and sends it to the `https://developer.android.com/reference/android/media/MediaCodec.html` to render it on the output surface. If the `https://developer.android.com/reference/android/media/MediaCodec.html` is not configured with an output surface, this call will simply return the buffer to the `https://developer.android.com/reference/android/media/MediaCodec.html`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#releaseOutputBuffer(int, long)` |   |

### setOnFrameRenderedListener

```
abstract void setOnFrameRenderedListener(
    MediaCodecAdapter.OnFrameRenderedListener listener,
    Handler handler
)
```

Registers a callback to be invoked when an output frame is rendered on the output surface.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#setOnFrameRenderedListener(android.media.MediaCodec.OnFrameRenderedListener, android.os.Handler)` |   |

### setOutputSurface

```
abstract void setOutputSurface(Surface surface)
```

Dynamically sets the output surface of a `https://developer.android.com/reference/android/media/MediaCodec.html`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#setOutputSurface(android.view.Surface)` |   |

### setParameters

```
abstract void setParameters(Bundle params)
```

Communicate additional parameter changes to the `https://developer.android.com/reference/android/media/MediaCodec.html` instance.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#setParameters(android.os.Bundle)` |   |

### setVideoScalingMode

```
abstract void setVideoScalingMode(@C.VideoScalingMode int scalingMode)
```

Specifies the scaling mode to use, if a surface was specified when the codec was created.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#setVideoScalingMode(int)` |   |

### subscribeToVendorParameters

```
@RequiresApi(value = 31)
abstract void subscribeToVendorParameters(List<String> names)
```

Subscribe to vendor parameters, so that these parameters will be present in `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputFormat()` and changes to these parameters generate output format change event.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#subscribeToVendorParameters(java.util.List&lt;java.lang.String&gt;)` |   |

### unsubscribeFromVendorParameters

```
@RequiresApi(value = 31)
abstract void unsubscribeFromVendorParameters(List<String> names)
```

Unsubscribe from vendor parameters, so that these parameters will not be present in `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#getOutputFormat()` and changes to these parameters no longer generate output format change event.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaCodec.html#unsubscribeFromVendorParameters(java.util.List&lt;java.lang.String&gt;)` |   |

### useBuffer

```
default void <E extends Exception> useBuffer(ThrowingRunnable<E> runnable)
```

Uses an acquired input or output buffer (e.g. to read from or write data to it).

This can be used for error handling, e.g. to guarantee no operation can invalidate the buffer while it's being used by the application or downstream components.

| Parameters |
|---|---|
| `<E extends https://developer.android.com/reference/java/lang/Exception.html>` | The type of exception thrown by `runnable`. |
| `https://developer.android.com/reference/androidx/media3/common/util/ThrowingRunnable<E> runnable` | The `https://developer.android.com/reference/androidx/media3/common/util/ThrowingRunnable` utilizing an acquired buffer. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/mediacodec/MediaCodecAdapter#useBuffer(androidx.media3.common.util.ThrowingRunnable<E>)` | If `runnable` throws an exception. |