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 = )The seeking mode. |
Constants |
|
|---|---|
const Int |
See |
const Int |
See |
const Int |
Public constructors |
|---|
MediaExtractorCompat(context: Context!)Creates a new instance. |
MediaExtractorCompat(Creates a new instance using the given |
Public functions |
|
|---|---|
Boolean |
advance()Advances to the next sample. |
Allocator! |
@VisibleForTesting(otherwise = 5) |
Long |
Returns an estimate of how much data is presently cached in memory, expressed in microseconds, or -1 if this information is unavailable or not applicable (i.e., no cache exists). |
DrmInitData? |
Extracts the DRM initialization data from the available tracks, if it exists. |
LogSessionId! |
@RequiresApi(value = 31)Returns the |
PersistableBundle! |
@RequiresApi(value = 26)Returns a |
(Mutable)Map<UUID!, ByteArray<Byte>!>? |
Extracts PSSH data from the media, if present. |
Boolean |
Returns |
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(trackIndex: Int)Returns the track |
Boolean |
Returns |
Int |
readSampleData(buffer: ByteBuffer!, offset: Int)Retrieves the current encoded sample and stores it in the byte |
Unit |
release()Releases any resources held by this instance. |
Unit |
seekTo(timeUs: Long, @MediaExtractorCompat.SeekMode mode: Int)All selected tracks seek near the requested |
Unit |
selectTrack(trackIndex: Int)Selects a track at the specified |
Unit |
setDataSource(assetFileDescriptor: AssetFileDescriptor!)Sets the data source using the media stream obtained from the provided |
Unit |
setDataSource(fileDescriptor: FileDescriptor!)Sets the data source using the media stream obtained from the provided |
Unit |
setDataSource(mediaDataSource: MediaDataSource!)Sets the data source using the media stream obtained from the given |
Unit |
setDataSource(path: String!)Sets the data source using the media stream obtained from the given file path or HTTP URL. |
Unit |
Sets the data source using the media stream obtained from the given file path or HTTP URL, with optional HTTP request headers. |
Unit |
setDataSource(uri: Uri!, offset: Long)Sets the data source using the media stream obtained from the given |
Unit |
Sets the data source using the media stream obtained from the given |
Unit |
setDataSource(fileDescriptor: FileDescriptor!, offset: Long, length: Long)Sets the data source using the media stream obtained from the provided |
Unit |
@RequiresApi(value = 31)Sets the |
Unit |
unselectTrack(trackIndex: Int)Unselects the track at the specified |
Constants
Public constructors
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 DataSource.Factory.
Note: The DataSource.Factory provided will not be used to generate DataSource 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 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.
getCachedDuration
fun getCachedDuration(): Long
Returns an estimate of how much data is presently cached in memory, expressed in microseconds, or -1 if this information is unavailable or not applicable (i.e., no cache exists).
getDrmInitData
fun getDrmInitData(): DrmInitData?
Extracts the DRM initialization data from the available tracks, if it exists.
| Returns | |
|---|---|
DrmInitData? |
The |
getLogSessionId
@RequiresApi(value = 31)
fun getLogSessionId(): LogSessionId!
Returns the LogSessionId for MediaExtractorCompat.
getMetrics
@RequiresApi(value = 26)
fun getMetrics(): PersistableBundle!
Returns a PersistableBundle containing metrics data for the current media container.
The bundle includes attributes and values for the media container, as described in MediaExtractor.MetricsConstants.
getPsshInfo
fun getPsshInfo(): (Mutable)Map<UUID!, ByteArray<Byte>!>?
Extracts PSSH data from the media, if present.
getSampleCryptoInfo
fun getSampleCryptoInfo(info: MediaCodec.CryptoInfo!): Boolean
Returns true if the current sample is at least partially encrypted and fills the provided MediaCodec.CryptoInfo structure with relevant decryption information.
| Parameters | |
|---|---|
info: MediaCodec.CryptoInfo! |
The |
| Returns | |
|---|---|
Boolean |
|
getSampleSize
fun getSampleSize(): Long
Returns the current sample's size in bytes, or -1 if no more samples are available.
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.
getTrackFormat
fun getTrackFormat(trackIndex: Int): MediaFormat!
Returns the track MediaFormat at the specified trackIndex.
hasCacheReachedEndOfStream
fun hasCacheReachedEndOfStream(): Boolean
Returns true if data is being cached and the cache has reached the end of the data stream. This indicates that no additional data is currently available for caching, although a future seek may restart data fetching. This method only returns a meaningful result if getCachedDuration indicates the presence of a cache (i.e., does not return -1).
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(assetFileDescriptor: AssetFileDescriptor!): Unit
Sets the data source using the media stream obtained from the provided AssetFileDescriptor.
Note: The caller is responsible for closing the AssetFileDescriptor. It is safe to do so immediately after this method returns.
| Parameters | |
|---|---|
assetFileDescriptor: AssetFileDescriptor! |
The |
| 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
fun setDataSource(fileDescriptor: FileDescriptor!): Unit
Sets the data source using the media stream obtained from the provided FileDescriptor.
| Parameters | |
|---|---|
fileDescriptor: FileDescriptor! |
The |
| 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
fun setDataSource(mediaDataSource: MediaDataSource!): Unit
Sets the data source using the media stream obtained from the given MediaDataSource.
| Parameters | |
|---|---|
mediaDataSource: MediaDataSource! |
The |
| 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
fun setDataSource(path: String!): Unit
Sets the data source using the media stream obtained from the given file path or HTTP URL.
| Parameters | |
|---|---|
path: String! |
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
fun setDataSource(path: String!, headers: (Mutable)Map<String!, String!>?): Unit
Sets the data source using the media stream obtained from the given file path or HTTP URL, with optional HTTP request headers.
| Parameters | |
|---|---|
path: String! |
The path of the file, or the HTTP URL, to extract media from. |
headers: (Mutable)Map<String!, String!>? |
An optional |
| 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
fun setDataSource(uri: Uri!, offset: Long): Unit
Sets the data source using the media stream obtained from the given Uri at the given offset.
| Parameters | |
|---|---|
uri: Uri! |
The content |
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. |
setDataSource
fun setDataSource(
context: Context!,
uri: Uri!,
headers: (Mutable)Map<String!, String!>?
): Unit
Sets the data source using the media stream obtained from the given content URI and optional HTTP request headers.
| Parameters | |
|---|---|
context: Context! |
|
uri: Uri! |
The |
headers: (Mutable)Map<String!, String!>? |
An optional |
| 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
fun setDataSource(fileDescriptor: FileDescriptor!, offset: Long, length: Long): Unit
Sets the data source using the media stream obtained from the provided FileDescriptor, with a specified offset and length.
| Parameters | |
|---|---|
fileDescriptor: FileDescriptor! |
The |
offset: Long |
The offset into the file where the data to be extracted starts, in bytes. |
length: Long |
The length of the data to be extracted, in bytes, or |
| 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. |
setLogSessionId
@RequiresApi(value = 31)
fun setLogSessionId(logSessionId: LogSessionId!): Unit
Sets the LogSessionId for MediaExtractorCompat.
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.