MediaFormatUtil


@UnstableApi
public final class MediaFormatUtil


Helper class containing utility methods for managing MediaFormat instances.

Summary

Constants

static final String
KEY_MAX_BIT_RATE = "max-bitrate"

The MediaFormat key for the maximum bitrate in bits per second.

static final String
KEY_PCM_ENCODING_EXTENDED = "exo-pcm-encoding-int"

Custom MediaFormat key associated with an integer representing the PCM encoding.

static final String
KEY_PIXEL_WIDTH_HEIGHT_RATIO_FLOAT = "exo-pixel-width-height-ratio-float"

Custom MediaFormat key associated with a float representing the ratio between a pixel's width and height.

Public methods

static Format

Returns a Format representing the given MediaFormat.

static MediaFormat

Returns a MediaFormat representing the given ExoPlayer Format.

static byte[]
getArray(ByteBuffer byteBuffer)
static @Nullable ColorInfo

Creates and returns a ColorInfo, if a valid instance is described in the .

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

Supports getFloat for API < 29.

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

Supports getInteger for API < 29.

static @Nullable String
getString(
    MediaFormat mediaFormat,
    String name,
    @Nullable String defaultValue
)

Supports getString for API < 29.

static @Nullable Integer

Returns the time lapse capture FPS from the given MediaFormat if it was set.

static boolean

Returns whether a MediaFormat is an audio format.

static boolean

Returns whether a MediaFormat is a video format.

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

Sets a MediaFormatByteBuffer value.

static void

Sets a MediaFormat's color information.

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

Sets a MediaFormat float value.

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

Sets a MediaFormat integer value.

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

Sets a MediaFormatString value.

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

Sets a MediaFormat's codec specific data buffers.

Constants

KEY_MAX_BIT_RATE

public static final String KEY_MAX_BIT_RATE = "max-bitrate"

The MediaFormat 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 MediaFormat key associated with an integer representing the PCM encoding.

Equivalent to KEY_PCM_ENCODING, except it allows additional values defined by C.PcmEncoding, including ENCODING_PCM_16BIT_BIG_ENDIAN, ENCODING_PCM_24BIT, ENCODING_PCM_24BIT_BIG_ENDIAN, ENCODING_PCM_32BIT and 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 MediaFormat 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 Format representing the given MediaFormat.

createMediaFormatFromFormat

public static MediaFormat createMediaFormatFromFormat(Format format)

Returns a MediaFormat representing the given ExoPlayer Format.

May include the following custom keys:

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 .

Under API 24, null will always be returned, because MediaFormat color keys like KEY_COLOR_STANDARD were only added in API 24.

getFloat

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

Supports getFloat for API < 29.

getInteger

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

Supports getInteger for API < 29.

getString

public static @Nullable String getString(
    MediaFormat mediaFormat,
    String name,
    @Nullable String defaultValue
)

Supports getString for API < 29.

getTimeLapseFrameRate

public static @Nullable Integer getTimeLapseFrameRate(MediaFormat format)

Returns the time lapse capture FPS from the given MediaFormat if it was set.

isAudioFormat

public static boolean isAudioFormat(MediaFormat mediaFormat)

Returns whether a MediaFormat is an audio format.

isVideoFormat

public static boolean isVideoFormat(MediaFormat mediaFormat)

Returns whether a MediaFormat is a video format.

maybeSetByteBuffer

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

Sets a MediaFormatByteBuffer value. Does nothing if value is null.

Parameters
MediaFormat format

The MediaFormat being configured.

String key

The key to set.

@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 MediaFormat's color information. Does nothing if colorInfo is null.

Parameters
MediaFormat format

The MediaFormat being configured.

@Nullable ColorInfo colorInfo

The color info to set.

maybeSetFloat

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

Sets a MediaFormat float value. Does nothing if value is NO_VALUE.

Parameters
MediaFormat format

The MediaFormat being configured.

String key

The key to set.

float value

The value to set.

maybeSetInteger

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

Sets a MediaFormat integer value. Does nothing if value is NO_VALUE.

Parameters
MediaFormat format

The MediaFormat being configured.

String 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 MediaFormatString value. Does nothing if value is null.

Parameters
MediaFormat format

The MediaFormat being configured.

String key

The key to set.

@Nullable String value

The value to set.

setCsdBuffers

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

Sets a MediaFormat's codec specific data buffers.

Parameters
MediaFormat format

The MediaFormat being configured.

List<byte[]> csdBuffers

The csd buffers to set.