# MediaFormatUtil

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

*** ** * ** ***


```
@UnstableApi
class MediaFormatUtil
```

<br />

*** ** * ** ***

Helper class containing utility methods for managing `https://developer.android.com/reference/android/media/MediaFormat.html` instances.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#KEY_MAX_BIT_RATE() = "max-bitrate"` The `https://developer.android.com/reference/android/media/MediaFormat.html` key for the maximum bitrate in bits per second. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#KEY_PCM_ENCODING_EXTENDED() = "exo-pcm-encoding-int"` Custom `https://developer.android.com/reference/android/media/MediaFormat.html` key associated with an integer representing the PCM encoding. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#KEY_PIXEL_WIDTH_HEIGHT_RATIO_FLOAT() = "exo-pixel-width-height-ratio-float"` Custom `https://developer.android.com/reference/android/media/MediaFormat.html` key associated with a float representing the ratio between a pixel's width and height. |

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/media3/common/Format!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#createFormatFromMediaFormat(android.media.MediaFormat)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!)` Returns a `https://developer.android.com/reference/kotlin/androidx/media3/common/Format` representing the given `https://developer.android.com/reference/android/media/MediaFormat.html`. |
| `java-static https://developer.android.com/reference/android/media/MediaFormat.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#createMediaFormatFromFormat(androidx.media3.common.Format)(format: https://developer.android.com/reference/kotlin/androidx/media3/common/Format!)` Returns a `https://developer.android.com/reference/android/media/MediaFormat.html` representing the given ExoPlayer `https://developer.android.com/reference/kotlin/androidx/media3/common/Format`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getArray(java.nio.ByteBuffer)(byteBuffer: https://developer.android.com/reference/java/nio/ByteBuffer.html!)` |
| `java-static https://developer.android.com/reference/kotlin/androidx/media3/common/ColorInfo?` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getColorInfo(android.media.MediaFormat)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!)` Creates and returns a `ColorInfo`, if a valid instance is described in the `https://developer.android.com/reference/android/media/MediaFormat.html`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getFloat(android.media.MediaFormat,java.lang.String,float)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!, name: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, defaultValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getFloat(java.lang.String, float)` for `API < 29`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getFloatFromIntOrFloat(android.media.MediaFormat,java.lang.String,float)( mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!, keyName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, defaultValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html )` Returns the value of a key whose value can be set as int or float. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getInteger(android.media.MediaFormat,java.lang.String,int)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!, name: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, defaultValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getInteger(java.lang.String, int)` for `API < 29`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getString(android.media.MediaFormat,java.lang.String,java.lang.String)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!, name: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, defaultValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!)` Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getString(java.lang.String, java.lang.String)` for `API < 29`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html?` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#getTimeLapseFrameRate(android.media.MediaFormat)(format: https://developer.android.com/reference/android/media/MediaFormat.html!)` Returns the time lapse capture FPS from the given `https://developer.android.com/reference/android/media/MediaFormat.html` if it was set. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#isAudioFormat(android.media.MediaFormat)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!)` Returns whether a `https://developer.android.com/reference/android/media/MediaFormat.html` is an audio format. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#isVideoFormat(android.media.MediaFormat)(mediaFormat: https://developer.android.com/reference/android/media/MediaFormat.html!)` Returns whether a `https://developer.android.com/reference/android/media/MediaFormat.html` is a video format. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#maybeSetByteBuffer(android.media.MediaFormat,java.lang.String,byte[])( format: https://developer.android.com/reference/android/media/MediaFormat.html!, key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>? )` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html``https://developer.android.com/reference/java/nio/ByteBuffer.html` value. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#maybeSetColorInfo(android.media.MediaFormat,androidx.media3.common.ColorInfo)(format: https://developer.android.com/reference/android/media/MediaFormat.html!, colorInfo: https://developer.android.com/reference/kotlin/androidx/media3/common/ColorInfo?)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html`'s color information. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#maybeSetFloat(android.media.MediaFormat,java.lang.String,float)(format: https://developer.android.com/reference/android/media/MediaFormat.html!, key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html` float value. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#maybeSetInteger(android.media.MediaFormat,java.lang.String,int)(format: https://developer.android.com/reference/android/media/MediaFormat.html!, key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html` integer value. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#maybeSetString(android.media.MediaFormat,java.lang.String,java.lang.String)(format: https://developer.android.com/reference/android/media/MediaFormat.html!, key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html``https://developer.android.com/reference/java/lang/String.html` value. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#setCsdBuffers(android.media.MediaFormat,java.util.List<byte[]>)( format: https://developer.android.com/reference/android/media/MediaFormat.html!, csdBuffers: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!>! )` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html`'s codec specific data buffers. |

## Constants

### KEY_MAX_BIT_RATE

```
const val KEY_MAX_BIT_RATE = "max-bitrate": String!
```

The `https://developer.android.com/reference/android/media/MediaFormat.html` key for the maximum bitrate in bits per second.

The associated value is an integer.

The key string constant is the same as `MediaFormat#KEY_MAX_BITRATE`. Values for it are already returned by the framework MediaExtractor; the key is a hidden field in `
MediaFormat` though, which is why it's being replicated here.

### KEY_PCM_ENCODING_EXTENDED

```
const val KEY_PCM_ENCODING_EXTENDED = "exo-pcm-encoding-int": String!
```

Custom `https://developer.android.com/reference/android/media/MediaFormat.html` key associated with an integer representing the PCM encoding.

Equivalent to `https://developer.android.com/reference/android/media/MediaFormat.html#KEY_PCM_ENCODING`, except it allows additional values defined by `https://developer.android.com/reference/kotlin/androidx/media3/common/C.PcmEncoding`, including `https://developer.android.com/reference/kotlin/androidx/media3/common/C#ENCODING_PCM_16BIT_BIG_ENDIAN()`, `https://developer.android.com/reference/kotlin/androidx/media3/common/C#ENCODING_PCM_24BIT()`, `https://developer.android.com/reference/kotlin/androidx/media3/common/C#ENCODING_PCM_24BIT_BIG_ENDIAN()`, `https://developer.android.com/reference/kotlin/androidx/media3/common/C#ENCODING_PCM_32BIT()` and `https://developer.android.com/reference/kotlin/androidx/media3/common/C#ENCODING_PCM_32BIT_BIG_ENDIAN()`.

### KEY_PIXEL_WIDTH_HEIGHT_RATIO_FLOAT

```
const val KEY_PIXEL_WIDTH_HEIGHT_RATIO_FLOAT = "exo-pixel-width-height-ratio-float": String!
```

Custom `https://developer.android.com/reference/android/media/MediaFormat.html` key associated with a float representing the ratio between a pixel's width and height.

## Public functions

### createFormatFromMediaFormat

```
java-static fun createFormatFromMediaFormat(mediaFormat: MediaFormat!): Format!
```

Returns a `https://developer.android.com/reference/kotlin/androidx/media3/common/Format` representing the given `https://developer.android.com/reference/android/media/MediaFormat.html`.

### createMediaFormatFromFormat

```
java-static fun createMediaFormatFromFormat(format: Format!): MediaFormat!
```

Returns a `https://developer.android.com/reference/android/media/MediaFormat.html` representing the given ExoPlayer `https://developer.android.com/reference/kotlin/androidx/media3/common/Format`.

May include the following custom keys:

- `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#KEY_PIXEL_WIDTH_HEIGHT_RATIO_FLOAT()`.
- `https://developer.android.com/reference/kotlin/androidx/media3/common/util/MediaFormatUtil#KEY_PCM_ENCODING_EXTENDED()`.

### getArray

```
java-static fun getArray(byteBuffer: ByteBuffer!): Array<Byte>!
```

### getColorInfo

```
java-static fun getColorInfo(mediaFormat: MediaFormat!): ColorInfo?
```

Creates and returns a `ColorInfo`, if a valid instance is described in the `https://developer.android.com/reference/android/media/MediaFormat.html`.

### getFloat

```
java-static fun getFloat(mediaFormat: MediaFormat!, name: String!, defaultValue: Float): Float
```

Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getFloat(java.lang.String, float)` for `API < 29`.

### getFloatFromIntOrFloat

```
java-static fun getFloatFromIntOrFloat(
    mediaFormat: MediaFormat!,
    keyName: String!,
    defaultValue: Float
): Float
```

Returns the value of a key whose value can be set as int or float.

The `defaultValue` is returned if the key is not present in the `https://developer.android.com/reference/android/media/MediaFormat.html`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ClassCastException.html` | If the stored value for the key is other than int or float. |

### getInteger

```
java-static fun getInteger(mediaFormat: MediaFormat!, name: String!, defaultValue: Int): Int
```

Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getInteger(java.lang.String, int)` for `API < 29`.

### getString

```
java-static fun getString(mediaFormat: MediaFormat!, name: String!, defaultValue: String!): String!
```

Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getString(java.lang.String, java.lang.String)` for `API < 29`.

### getTimeLapseFrameRate

```
java-static fun getTimeLapseFrameRate(format: MediaFormat!): Int?
```

Returns the time lapse capture FPS from the given `https://developer.android.com/reference/android/media/MediaFormat.html` if it was set.

### isAudioFormat

```
java-static fun isAudioFormat(mediaFormat: MediaFormat!): Boolean
```

Returns whether a `https://developer.android.com/reference/android/media/MediaFormat.html` is an audio format.

### isVideoFormat

```
java-static fun isVideoFormat(mediaFormat: MediaFormat!): Boolean
```

Returns whether a `https://developer.android.com/reference/android/media/MediaFormat.html` is a video format.

### maybeSetByteBuffer

```
java-static fun maybeSetByteBuffer(
    format: MediaFormat!,
    key: String!,
    value: ByteArray<Byte>?
): Unit
```

Sets a `https://developer.android.com/reference/android/media/MediaFormat.html``https://developer.android.com/reference/java/nio/ByteBuffer.html` value. Does nothing if `value` is null.

| Parameters |
|---|---|
| `format: https://developer.android.com/reference/android/media/MediaFormat.html!` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The key to set. |
| `value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>?` | The byte array that will be wrapped to obtain the value. |

### maybeSetColorInfo

```
java-static fun maybeSetColorInfo(format: MediaFormat!, colorInfo: ColorInfo?): Unit
```

Sets a `https://developer.android.com/reference/android/media/MediaFormat.html`'s color information. Does nothing if `colorInfo` is null.

| Parameters |
|---|---|
| `format: https://developer.android.com/reference/android/media/MediaFormat.html!` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `colorInfo: https://developer.android.com/reference/kotlin/androidx/media3/common/ColorInfo?` | The color info to set. |

### maybeSetFloat

```
java-static fun maybeSetFloat(format: MediaFormat!, key: String!, value: Float): Unit
```

Sets a `https://developer.android.com/reference/android/media/MediaFormat.html` float value. Does nothing if `value` is `https://developer.android.com/reference/kotlin/androidx/media3/common/Format#NO_VALUE()`.

| Parameters |
|---|---|
| `format: https://developer.android.com/reference/android/media/MediaFormat.html!` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The key to set. |
| `value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | The value to set. |

### maybeSetInteger

```
java-static fun maybeSetInteger(format: MediaFormat!, key: String!, value: Int): Unit
```

Sets a `https://developer.android.com/reference/android/media/MediaFormat.html` integer value. Does nothing if `value` is `https://developer.android.com/reference/kotlin/androidx/media3/common/Format#NO_VALUE()`.

| Parameters |
|---|---|
| `format: https://developer.android.com/reference/android/media/MediaFormat.html!` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The key to set. |
| `value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The value to set. |

### maybeSetString

```
java-static fun maybeSetString(format: MediaFormat!, key: String!, value: String?): Unit
```

Sets a `https://developer.android.com/reference/android/media/MediaFormat.html``https://developer.android.com/reference/java/lang/String.html` value. Does nothing if `value` is null.

| Parameters |
|---|---|
| `format: https://developer.android.com/reference/android/media/MediaFormat.html!` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The key to set. |
| `value: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The value to set. |

### setCsdBuffers

```
java-static fun setCsdBuffers(
    format: MediaFormat!,
    csdBuffers: (Mutable)List<ByteArray<Byte>!>!
): Unit
```

Sets a `https://developer.android.com/reference/android/media/MediaFormat.html`'s codec specific data buffers.

| Parameters |
|---|---|
| `format: https://developer.android.com/reference/android/media/MediaFormat.html!` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `csdBuffers: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!>!` | The csd buffers to set. |