MediaExtractorCompat


@UnstableApi
public final class MediaExtractorCompat


A drop-in replacement for MediaExtractor that provides similar functionality, based on the media3.extractor logic.

Summary

Nested types

@IntDef(value = )
@Retention(value = RetentionPolicy.SOURCE)
public annotation MediaExtractorCompat.SeekMode

The seeking mode.

Constants

static final int

See SEEK_TO_CLOSEST_SYNC.

static final int

See SEEK_TO_NEXT_SYNC.

static final int

See SEEK_TO_PREVIOUS_SYNC.

Public fields

final Allocator

Public constructors

Creates a new instance.

MediaExtractorCompat(
    ExtractorsFactory extractorsFactory,
    DataSource.Factory dataSourceFactory
)

Creates a new instance using the given ExtractorsFactory to create the extractors to use for obtaining media samples from a DataSource generated by the given DataSource.Factory.

Public methods

boolean

Advances to the next sample.

Allocator
@VisibleForTesting(otherwise = 5)
getAllocator()
int

Returns the current sample's flags.

long

Returns the current sample's size in bytes, or -1 if no more samples are available.

long

Returns the current sample's presentation time in microseconds, or -1 if no more samples are available.

int

Returns the track index the current sample originates from, or -1 if no more samples are available.

int

Returns the number of tracks found in the data source.

MediaFormat
getTrackFormat(int trackIndex)

Returns the track MediaFormat at the specified trackIndex.

int
readSampleData(ByteBuffer buffer, int offset)

Retrieves the current encoded sample and stores it in the byte buffer starting at the given offset.

void

Releases any resources held by this instance.

void
seekTo(long timeUs, @MediaExtractorCompat.SeekMode int mode)

All selected tracks seek near the requested timeUs according to the specified mode.

void
selectTrack(int trackIndex)

Selects a track at the specified trackIndex.

void
setDataSource(AssetFileDescriptor assetFileDescriptor)

Sets the data source using the media stream obtained from the provided .

void
setDataSource(FileDescriptor fileDescriptor)

Sets the data source using the media stream obtained from the provided FileDescriptor.

void
@RequiresApi(value = 23)
setDataSource(MediaDataSource mediaDataSource)

Sets the data source using the media stream obtained from the given MediaDataSource.

void

Sets the data source using the media stream obtained from the given file path or HTTP URL.

void

Sets the data source using the media stream obtained from the given file path or HTTP URL, with optional HTTP request headers.

void
setDataSource(Uri uri, long offset)

Sets the data source using the media stream obtained from the given Uri at the given offset.

void
setDataSource(
    Context context,
    Uri uri,
    @Nullable Map<StringString> headers
)

Sets the data source using the media stream obtained from the given content URI and optional HTTP request headers.

void
setDataSource(FileDescriptor fileDescriptor, long offset, long length)

Sets the data source using the media stream obtained from the provided FileDescriptor, with a specified offset and length.

void
unselectTrack(int trackIndex)

Unselects the track at the specified trackIndex.

Constants

SEEK_TO_CLOSEST_SYNC

public static final int SEEK_TO_CLOSEST_SYNC = 2

See SEEK_TO_CLOSEST_SYNC.

SEEK_TO_NEXT_SYNC

public static final int SEEK_TO_NEXT_SYNC = 1

See SEEK_TO_NEXT_SYNC.

SEEK_TO_PREVIOUS_SYNC

public static final int SEEK_TO_PREVIOUS_SYNC = 0

See SEEK_TO_PREVIOUS_SYNC.

Public fields

allocator

public final Allocator allocator

Public constructors

MediaExtractorCompat

public MediaExtractorCompat(Context context)

Creates a new instance.

MediaExtractorCompat

public MediaExtractorCompat(
    ExtractorsFactory extractorsFactory,
    DataSource.Factory dataSourceFactory
)

Creates a new instance using the given ExtractorsFactory to create the extractors to use for obtaining media samples from a DataSource generated by the given DataSource.Factory.

Note: The DataSource.Factory provided will not be used to generate when setting data source using methods:

Note: The DataSource.Factory provided may not be used to generate DataSource when setting data source using method setDataSource as the behavior depends on the fallthrough logic related to the scheme of the provided URI.

Public methods

advance

public boolean advance()

Advances to the next sample. Returns false if no more sample data is available (i.e., end of stream), or true otherwise.

Note: When extracting from a local file, the behavior of advance and readSampleData is undefined if there are concurrent writes to the same file. This may result in an unexpected end of stream being signaled.

getAllocator

@VisibleForTesting(otherwise = 5)
public Allocator getAllocator()

getSampleFlags

public int getSampleFlags()

Returns the current sample's flags.

getSampleSize

public long getSampleSize()

Returns the current sample's size in bytes, or -1 if no more samples are available.

getSampleTime

public long getSampleTime()

Returns the current sample's presentation time in microseconds, or -1 if no more samples are available.

getSampleTrackIndex

public int getSampleTrackIndex()

Returns the track index the current sample originates from, or -1 if no more samples are available.

getTrackCount

public int getTrackCount()

Returns the number of tracks found in the data source.

getTrackFormat

public MediaFormat getTrackFormat(int trackIndex)

Returns the track MediaFormat at the specified trackIndex.

readSampleData

public int readSampleData(ByteBuffer buffer, int offset)

Retrieves the current encoded sample and stores it in the byte buffer starting at the given offset.

Note:On success, the position and limit of buffer is updated to point to the data just read.

Parameters
ByteBuffer buffer

the destination byte buffer.

int offset

The offset into the byte buffer at which to write.

Returns
int

the sample size, or -1 if no more samples are available.

release

public void release()

Releases any resources held by this instance.

Note: Make sure you call this when you're done to free up any resources instead of relying on the garbage collector to do this for you at some point in the future.

seekTo

public void seekTo(long timeUs, @MediaExtractorCompat.SeekMode int mode)

All selected tracks seek near the requested timeUs according to the specified mode.

selectTrack

public void selectTrack(int trackIndex)

Selects a track at the specified trackIndex.

Subsequent calls to readSampleData, getSampleTrackIndex and getSampleTime only retrieve information for the subset of tracks selected.

Note: Selecting the same track multiple times has no effect, the track is only selected once.

setDataSource

public void setDataSource(AssetFileDescriptor assetFileDescriptor)

Sets the data source using the media stream obtained from the provided .

Note: The caller is responsible for closing the AssetFileDescriptor. It is safe to do so immediately after this method returns.

Parameters
AssetFileDescriptor assetFileDescriptor

The AssetFileDescriptor for the file to extract from.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

public void setDataSource(FileDescriptor fileDescriptor)

Sets the data source using the media stream obtained from the provided FileDescriptor.

Parameters
FileDescriptor fileDescriptor

The FileDescriptor for the file to extract from.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

@RequiresApi(value = 23)
public void setDataSource(MediaDataSource mediaDataSource)

Sets the data source using the media stream obtained from the given MediaDataSource.

Parameters
MediaDataSource mediaDataSource

The MediaDataSource to extract media from.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

public void setDataSource(String path)

Sets the data source using the media stream obtained from the given file path or HTTP URL.

Parameters
String path

The path of the file, or the HTTP URL, to extract media from.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

public void setDataSource(String path, @Nullable Map<StringString> headers)

Sets the data source using the media stream obtained from the given file path or HTTP URL, with optional HTTP request headers.

Parameters
String path

The path of the file, or the HTTP URL, to extract media from.

@Nullable Map<StringString> headers

An optional Map of HTTP request headers to include when fetching the data, or null if no headers are to be included.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

public void setDataSource(Uri uri, long offset)

Sets the data source using the media stream obtained from the given Uri at the given offset.

Parameters
Uri uri

The content Uri to extract from.

long offset

The offset into the file where the data to be extracted starts, in bytes.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

public void setDataSource(
    Context context,
    Uri uri,
    @Nullable Map<StringString> headers
)

Sets the data source using the media stream obtained from the given content URI and optional HTTP request headers.

Parameters
Context context

The Context used to resolve the Uri.

Uri uri

The content URI of the media to extract from.

@Nullable Map<StringString> headers

An optional Map of HTTP request headers to include when fetching the data, or null if no headers are to be included.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

setDataSource

public void setDataSource(FileDescriptor fileDescriptor, long offset, long length)

Sets the data source using the media stream obtained from the provided FileDescriptor, with a specified offset and length.

Parameters
FileDescriptor fileDescriptor

The FileDescriptor for the file to extract from.

long offset

The offset into the file where the data to be extracted starts, in bytes.

long length

The length of the data to be extracted, in bytes, or LENGTH_UNSET if it is unknown.

Throws
java.io.IOException

If an error occurs while extracting the media.

androidx.media3.exoplayer.source.UnrecognizedInputFormatException

If none of the available extractors successfully sniffs the input.

java.lang.IllegalStateException

If this method is called twice on the same instance.

unselectTrack

public void unselectTrack(int trackIndex)

Unselects the track at the specified trackIndex.

Subsequent calls to readSampleData, getSampleTrackIndex and getSampleTime only retrieve information for the subset of tracks selected.