# 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
public final class MediaFormatUtil
```

<br />

*** ** * ** ***

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

## Summary

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/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. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/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. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/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 methods |
|---|---|
| `static https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#createFormatFromMediaFormat(android.media.MediaFormat)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat)` Returns a `https://developer.android.com/reference/androidx/media3/common/Format` representing the given `https://developer.android.com/reference/android/media/MediaFormat.html`. |
| `static https://developer.android.com/reference/android/media/MediaFormat.html` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#createMediaFormatFromFormat(androidx.media3.common.Format)(https://developer.android.com/reference/androidx/media3/common/Format format)` Returns a `https://developer.android.com/reference/android/media/MediaFormat.html` representing the given ExoPlayer `https://developer.android.com/reference/androidx/media3/common/Format`. |
| `static byte[]` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getArray(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer.html byteBuffer)` |
| `static @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/ColorInfo` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getColorInfo(android.media.MediaFormat)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat)` Creates and returns a `ColorInfo`, if a valid instance is described in the `https://developer.android.com/reference/android/media/MediaFormat.html`. |
| `static float` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getFloat(android.media.MediaFormat,java.lang.String,float)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat, https://developer.android.com/reference/java/lang/String.html name, float defaultValue)` Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getFloat(java.lang.String, float)` for `API < 29`. |
| `static float` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getFloatFromIntOrFloat(android.media.MediaFormat,java.lang.String,float)( https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat, https://developer.android.com/reference/java/lang/String.html keyName, float defaultValue )` Returns the value of a key whose value can be set as int or float. |
| `static int` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getInteger(android.media.MediaFormat,java.lang.String,int)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat, https://developer.android.com/reference/java/lang/String.html name, int defaultValue)` Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getInteger(java.lang.String, int)` for `API < 29`. |
| `static https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getString(android.media.MediaFormat,java.lang.String,java.lang.String)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat, https://developer.android.com/reference/java/lang/String.html name, https://developer.android.com/reference/java/lang/String.html defaultValue)` Supports `https://developer.android.com/reference/android/media/MediaFormat.html#getString(java.lang.String, java.lang.String)` for `API < 29`. |
| `static @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Integer.html` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#getTimeLapseFrameRate(android.media.MediaFormat)(https://developer.android.com/reference/android/media/MediaFormat.html format)` Returns the time lapse capture FPS from the given `https://developer.android.com/reference/android/media/MediaFormat.html` if it was set. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#isAudioFormat(android.media.MediaFormat)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat)` Returns whether a `https://developer.android.com/reference/android/media/MediaFormat.html` is an audio format. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#isVideoFormat(android.media.MediaFormat)(https://developer.android.com/reference/android/media/MediaFormat.html mediaFormat)` Returns whether a `https://developer.android.com/reference/android/media/MediaFormat.html` is a video format. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#maybeSetByteBuffer(android.media.MediaFormat,java.lang.String,byte[])(https://developer.android.com/reference/android/media/MediaFormat.html format, https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/Nullable byte[] value)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html``https://developer.android.com/reference/java/nio/ByteBuffer.html` value. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#maybeSetColorInfo(android.media.MediaFormat,androidx.media3.common.ColorInfo)(https://developer.android.com/reference/android/media/MediaFormat.html format, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/ColorInfo colorInfo)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html`'s color information. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#maybeSetFloat(android.media.MediaFormat,java.lang.String,float)(https://developer.android.com/reference/android/media/MediaFormat.html format, https://developer.android.com/reference/java/lang/String.html key, float value)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html` float value. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#maybeSetInteger(android.media.MediaFormat,java.lang.String,int)(https://developer.android.com/reference/android/media/MediaFormat.html format, https://developer.android.com/reference/java/lang/String.html key, int value)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html` integer value. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#maybeSetString(android.media.MediaFormat,java.lang.String,java.lang.String)(https://developer.android.com/reference/android/media/MediaFormat.html format, https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html value)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html``https://developer.android.com/reference/java/lang/String.html` value. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/util/MediaFormatUtil#setCsdBuffers(android.media.MediaFormat,java.util.List<byte[]>)(https://developer.android.com/reference/android/media/MediaFormat.html format, https://developer.android.com/reference/java/util/List.html<byte[]> csdBuffers)` Sets a `https://developer.android.com/reference/android/media/MediaFormat.html`'s codec specific data buffers. |

## Constants

### KEY_MAX_BIT_RATE

```
public static final String 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.

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

```
public static final String 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.

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/androidx/media3/common/C.PcmEncoding`, including `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_16BIT_BIG_ENDIAN()`, `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_24BIT()`, `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_24BIT_BIG_ENDIAN()`, `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_32BIT()` and `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_32BIT_BIG_ENDIAN()`.

### KEY_PIXEL_WIDTH_HEIGHT_RATIO_FLOAT

```
public static final String 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 methods

### createFormatFromMediaFormat

```
public static Format createFormatFromMediaFormat(MediaFormat mediaFormat)
```

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

### createMediaFormatFromFormat

```
public static MediaFormat createMediaFormatFromFormat(Format format)
```

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

May include the following custom keys:

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

### getArray

```
public static byte[] getArray(ByteBuffer byteBuffer)
```

### getColorInfo

```
public static @Nullable ColorInfo getColorInfo(MediaFormat mediaFormat)
```

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

### getFloat

```
public static float getFloat(MediaFormat mediaFormat, String name, float defaultValue)
```

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

### getFloatFromIntOrFloat

```
public static float getFloatFromIntOrFloat(
    MediaFormat mediaFormat,
    String keyName,
    float defaultValue
)
```

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

```
public static int getInteger(MediaFormat mediaFormat, String name, int defaultValue)
```

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

### getString

```
public static String getString(MediaFormat mediaFormat, String name, String defaultValue)
```

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

### getTimeLapseFrameRate

```
public static @Nullable Integer getTimeLapseFrameRate(MediaFormat format)
```

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

### isAudioFormat

```
public static boolean isAudioFormat(MediaFormat mediaFormat)
```

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

### isVideoFormat

```
public static boolean isVideoFormat(MediaFormat mediaFormat)
```

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

### maybeSetByteBuffer

```
public static void maybeSetByteBuffer(MediaFormat format, String key, @Nullable byte[] value)
```

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 |
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat.html format` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `https://developer.android.com/reference/java/lang/String.html key` | The key to set. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable byte[] value` | The byte array that will be wrapped to obtain the value. |

### maybeSetColorInfo

```
public static void maybeSetColorInfo(MediaFormat format, @Nullable ColorInfo colorInfo)
```

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

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

### maybeSetFloat

```
public static void maybeSetFloat(MediaFormat format, String key, float value)
```

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat.html format` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `https://developer.android.com/reference/java/lang/String.html key` | The key to set. |
| `float value` | The value to set. |

### maybeSetInteger

```
public static void maybeSetInteger(MediaFormat format, String key, int value)
```

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat.html format` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `https://developer.android.com/reference/java/lang/String.html key` | The key to set. |
| `int value` | The value to set. |

### maybeSetString

```
public static void maybeSetString(MediaFormat format, String key, @Nullable String value)
```

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 |
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat.html format` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `https://developer.android.com/reference/java/lang/String.html key` | The key to set. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html value` | The value to set. |

### setCsdBuffers

```
public static void setCsdBuffers(MediaFormat format, List<byte[]> csdBuffers)
```

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat.html format` | The `https://developer.android.com/reference/android/media/MediaFormat.html` being configured. |
| `https://developer.android.com/reference/java/util/List.html<byte[]> csdBuffers` | The csd buffers to set. |