MediaExtractorCompat


@UnstableApi
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)
annotation MediaExtractorCompat.SeekMode

The seeking mode.

Public constructors

Creates a new instance.

MediaExtractorCompat(
    extractorsFactory: ExtractorsFactory!,
    dataSourceFactory: DataSource.Factory!
)

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 dataSourceFactory.

Public functions

Boolean

Advances to the next sample.

Int

Returns the current sample's flags.

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(trackIndex: Int)

Returns the track MediaFormat at the specified trackIndex.

Int
readSampleData(buffer: ByteBuffer!, offset: Int)

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

Unit

Releases any resources held by this instance.

Unit

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

Unit
selectTrack(trackIndex: Int)

Selects a track at the specified trackIndex.

Unit
setDataSource(uri: Uri!, offset: Long)

Initializes the internal state with the media stream obtained from the given uri at the given offset.

Unit
unselectTrack(trackIndex: Int)

Unselects the track at the specified trackIndex.

Public properties

Allocator!

Constants

SEEK_TO_CLOSEST_SYNC

const val SEEK_TO_CLOSEST_SYNC = 2: Int

See SEEK_TO_CLOSEST_SYNC.

SEEK_TO_NEXT_SYNC

const val SEEK_TO_NEXT_SYNC = 1: Int

See SEEK_TO_NEXT_SYNC.

SEEK_TO_PREVIOUS_SYNC

const val SEEK_TO_PREVIOUS_SYNC = 0: Int

See SEEK_TO_PREVIOUS_SYNC.

Public constructors

MediaExtractorCompat

MediaExtractorCompat(context: Context!)

Creates a new instance.

MediaExtractorCompat

MediaExtractorCompat(
    extractorsFactory: ExtractorsFactory!,
    dataSourceFactory: DataSource.Factory!
)

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 dataSourceFactory.

Public functions

advance

fun advance(): Boolean

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.

getSampleFlags

fun getSampleFlags(): Int

Returns the current sample's flags.

getSampleTime

fun getSampleTime(): Long

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

getSampleTrackIndex

fun getSampleTrackIndex(): Int

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

getTrackCount

fun getTrackCount(): Int

Returns the number of tracks found in the data source.

getTrackFormat

fun getTrackFormat(trackIndex: Int): MediaFormat!

Returns the track MediaFormat at the specified trackIndex.

readSampleData

fun readSampleData(buffer: ByteBuffer!, offset: Int): Int

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
buffer: ByteBuffer!

the destination byte buffer.

offset: Int

The offset into the byte buffer at which to write.

Returns
Int

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

release

fun release(): Unit

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

fun seekTo(timeUs: Long, @MediaExtractorCompat.SeekMode mode: Int): Unit

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

selectTrack

fun selectTrack(trackIndex: Int): Unit

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

fun setDataSource(uri: Uri!, offset: Long): Unit

Initializes the internal state with the media stream obtained from the given uri at the given offset.

Parameters
uri: Uri!

The content Uri to extract from.

offset: Long

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.

unselectTrack

fun unselectTrack(trackIndex: Int): Unit

Unselects the track at the specified trackIndex.

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

Public properties

allocator

val allocatorAllocator!