Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# Callback

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/android/media/MediaCodec.Callback "View this page in Java")

```
abstract class Callback
```

|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||
| ↳ | [android.media.MediaCodec.Callback](https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#) |

MediaCodec callback interface. Used to notify the user asynchronously of various MediaCodec events.

## Summary

| Public constructors ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#Callback()()` <br /> |

| Public methods ||
|---|---|
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onCryptoError(android.media.MediaCodec,%20android.media.MediaCodec.CryptoException)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, e: https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoException)` Called only when MediaCodec encountered a crypto(decryption) error when using a decoder configured with CONFIGURE_FLAG_USE_CRYPTO_ASYNC flag along with crypto or descrambler object. |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onError(android.media.MediaCodec,%20android.media.MediaCodec.CodecException)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, e: https://developer.android.com/reference/kotlin/android/media/MediaCodec.CodecException)` Called when the MediaCodec encountered an error |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onInputBufferAvailable(android.media.MediaCodec,%20kotlin.Int)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, index: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` Called when an input buffer becomes available. |
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onMetricsFlushed(android.media.MediaCodec,%20android.os.PersistableBundle)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, metrics: https://developer.android.com/reference/kotlin/android/os/PersistableBundle.html#)` Called when the metrics for this codec have been flushed "mid-stream" due to the start of a new subsession during execution. |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onOutputBufferAvailable(android.media.MediaCodec,%20kotlin.Int,%20android.media.MediaCodec.BufferInfo)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, index: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, info: https://developer.android.com/reference/kotlin/android/media/MediaCodec.BufferInfo)` Called when an output buffer becomes available. |
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onOutputBuffersAvailable(android.media.MediaCodec,%20kotlin.Int,%20java.util.ArrayDeque)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, index: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, infos: https://developer.android.com/reference/kotlin/java/util/ArrayDeque.html#<https://developer.android.com/reference/kotlin/android/media/MediaCodec.BufferInfo!>)` Called when multiple access-units are available in the output. |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onOutputFormatChanged(android.media.MediaCodec,%20android.media.MediaFormat)(codec: https://developer.android.com/reference/kotlin/android/media/MediaCodec, format: https://developer.android.com/reference/kotlin/android/media/MediaFormat)` Called when the output format has changed |

## Public constructors

### Callback

```
Callback()
```

## Public methods

### onCryptoError

Added in [API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun onCryptoError(
    codec: MediaCodec, 
    e: MediaCodec.CryptoException
): Unit
```

Called only when MediaCodec encountered a crypto(decryption) error when using a decoder configured with CONFIGURE_FLAG_USE_CRYPTO_ASYNC flag along with crypto or descrambler object.

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `e` | [MediaCodec.CryptoException](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoException): The `https://developer.android.com/reference/kotlin/android/media/MediaCodec.CryptoException` object with error details. This value cannot be `null`. |

### onError

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
abstract fun onError(
    codec: MediaCodec, 
    e: MediaCodec.CodecException
): Unit
```

Called when the MediaCodec encountered an error

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `e` | [MediaCodec.CodecException](https://developer.android.com/reference/kotlin/android/media/MediaCodec.CodecException): The `https://developer.android.com/reference/kotlin/android/media/MediaCodec.CodecException` object describing the error. This value cannot be `null`. |

### onInputBufferAvailable

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
abstract fun onInputBufferAvailable(
    codec: MediaCodec, 
    index: Int
): Unit
```

Called when an input buffer becomes available.

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `index` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): The index of the available input buffer. |

### onMetricsFlushed

Added in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun onMetricsFlushed(
    codec: MediaCodec, 
    metrics: PersistableBundle
): Unit
```

Called when the metrics for this codec have been flushed "mid-stream" due to the start of a new subsession during execution.

A new codec subsession normally starts when the codec is reconfigured after stop(), but it can also happen mid-stream e.g. if the video size changes. When this happens, the metrics for the previous subsession are flushed, and `https://developer.android.com/reference/kotlin/android/media/MediaCodec#getMetrics()` will return the metrics for the new subsession.

For subsessions that begin due to a reconfiguration, the metrics for the prior subsession can be retrieved via `https://developer.android.com/reference/kotlin/android/media/MediaCodec#getMetrics()` prior to calling #configure.

When a new subsession begins "mid-stream", the metrics for the prior subsession are flushed just before the `https://developer.android.com/reference/kotlin/android/media/MediaCodec.Callback#onOutputFormatChanged(android.media.MediaCodec,%20android.media.MediaFormat)` event, so this **optional** callback is provided to be able to capture the final metrics for the previous subsession.

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `metrics` | [PersistableBundle](https://developer.android.com/reference/kotlin/android/os/PersistableBundle.html#): The flushed metrics for this codec. This is a `https://developer.android.com/reference/kotlin/android/os/PersistableBundle.html#` containing the set of attributes and values available for the media being handled by this instance of MediaCodec. The attributes are described in `https://developer.android.com/reference/kotlin/android/media/MediaCodec.MetricsConstants`. Additional vendor-specific fields may also be present. This value cannot be `null`. |

### onOutputBufferAvailable

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
abstract fun onOutputBufferAvailable(
    codec: MediaCodec, 
    index: Int, 
    info: MediaCodec.BufferInfo
): Unit
```

Called when an output buffer becomes available.

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `index` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): The index of the available output buffer. |
| `info` | [MediaCodec.BufferInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.BufferInfo): Info regarding the available output buffer `https://developer.android.com/reference/kotlin/android/media/MediaCodec.BufferInfo`. This value cannot be `null`. |

### onOutputBuffersAvailable

Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
open fun onOutputBuffersAvailable(
    codec: MediaCodec, 
    index: Int, 
    infos: ArrayDeque<MediaCodec.BufferInfo!>
): Unit
```

Called when multiple access-units are available in the output.

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `index` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): The index of the available output buffer. |
| `infos` | [ArrayDeque](https://developer.android.com/reference/kotlin/java/util/ArrayDeque.html#)\<[MediaCodec.BufferInfo](https://developer.android.com/reference/kotlin/android/media/MediaCodec.BufferInfo)!\>: Infos describing the available output buffer `https://developer.android.com/reference/kotlin/android/media/MediaCodec.BufferInfo`. Access units present in the output buffer are laid out contiguously without gaps and in order. This value cannot be `null`. |

### onOutputFormatChanged

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
abstract fun onOutputFormatChanged(
    codec: MediaCodec, 
    format: MediaFormat
): Unit
```

Called when the output format has changed

| Parameters ||
|---|---|
| `codec` | [MediaCodec](https://developer.android.com/reference/kotlin/android/media/MediaCodec): The MediaCodec object. This value cannot be `null`. |
| `format` | [MediaFormat](https://developer.android.com/reference/kotlin/android/media/MediaFormat): The new output format. This value cannot be `null`. |