Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/media/MediaExtractor#nestedclasses) \| [Constants](https://developer.android.com/reference/android/media/MediaExtractor#constants) \| [Ctors](https://developer.android.com/reference/android/media/MediaExtractor#pubctors) \| [Methods](https://developer.android.com/reference/android/media/MediaExtractor#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/media/MediaExtractor#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/MediaExtractor#inhmethods)

# MediaExtractor

*** ** * ** ***

` public final class MediaExtractor `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.MediaExtractor |

<br />

*** ** * ** ***

MediaExtractor facilitates extraction of demuxed, typically encoded, media data from a data source.

It is generally used like this:

```
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(...);
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
  MediaFormat format = extractor.getTrackFormat(i);
  String mime = format.getString(MediaFormat.KEY_MIME);
  if (weAreInterestedInThisTrack) {
    extractor.selectTrack(i);
  }
}
ByteBuffer inputBuffer = ByteBuffer.allocate(...)
while (extractor.readSampleData(inputBuffer, ...) >= 0) {
  int trackIndex = extractor.getSampleTrackIndex();
  long presentationTimeUs = extractor.getSampleTime();
  ...
  extractor.advance();
}

extractor.release();
extractor = null;
```

This class requires the `https://developer.android.com/reference/android/Manifest.permission#INTERNET` permission when used with network-based content.

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/media/MediaExtractor.CasInfo` Describes the conditional access system used to scramble a track. |
| ` class` | `https://developer.android.com/reference/android/media/MediaExtractor.MetricsConstants` |

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_ENCRYPTED` The sample is (at least partially) encrypted, see also the documentation for `https://developer.android.com/reference/android/media/MediaCodec#queueSecureInputBuffer(int,%20int,%20android.media.MediaCodec.CryptoInfo,%20long,%20int)` |
| `int` | `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_PARTIAL_FRAME` This indicates that the buffer only contains part of a frame, and the decoder should batch the data until a buffer without this flag appears before decoding the frame. |
| `int` | `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_SYNC` The sample is a sync sample (or in `https://developer.android.com/reference/android/media/MediaCodec`'s terminology it is a key frame.) |
| `int` | `https://developer.android.com/reference/android/media/MediaExtractor#SEEK_TO_CLOSEST_SYNC` If possible, seek to the sync sample closest to the specified time |
| `int` | `https://developer.android.com/reference/android/media/MediaExtractor#SEEK_TO_NEXT_SYNC` If possible, seek to a sync sample at or after the specified time |
| `int` | `https://developer.android.com/reference/android/media/MediaExtractor#SEEK_TO_PREVIOUS_SYNC` If possible, seek to a sync sample at or before the specified time |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/media/MediaExtractor#MediaExtractor()() ` |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/android/media/MediaExtractor#advance()() ` Advance to the next sample. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/AudioPresentation>` | ` https://developer.android.com/reference/android/media/MediaExtractor#getAudioPresentations(int)(int trackIndex) ` Get the list of available audio presentations for the track. |
| ` long` | ` https://developer.android.com/reference/android/media/MediaExtractor#getCachedDuration()() ` Returns an estimate of how much data is presently cached in memory expressed in microseconds. |
| ` https://developer.android.com/reference/android/media/MediaExtractor.CasInfo` | ` https://developer.android.com/reference/android/media/MediaExtractor#getCasInfo(int)(int index) ` Retrieves the information about the conditional access system used to scramble a track. |
| ` https://developer.android.com/reference/android/media/DrmInitData` | ` https://developer.android.com/reference/android/media/MediaExtractor#getDrmInitData()() ` Extract DRM initialization data if it exists If the media contains a PSSH box, only PSSH version 0 is supported. |
| ` https://developer.android.com/reference/android/media/metrics/LogSessionId` | ` https://developer.android.com/reference/android/media/MediaExtractor#getLogSessionId()() ` Returns the `https://developer.android.com/reference/android/media/metrics/LogSessionId` for MediaExtractor. |
| ` https://developer.android.com/reference/android/os/PersistableBundle` | ` https://developer.android.com/reference/android/media/MediaExtractor#getMetrics()() ` Return Metrics data about the current media container. |
| ` https://developer.android.com/reference/java/util/Map<https://developer.android.com/reference/java/util/UUID, byte[]>` | ` https://developer.android.com/reference/android/media/MediaExtractor#getPsshInfo()() ` Get the PSSH info if present. |
| ` boolean` | ` https://developer.android.com/reference/android/media/MediaExtractor#getSampleCryptoInfo(android.media.MediaCodec.CryptoInfo)(https://developer.android.com/reference/android/media/MediaCodec.CryptoInfo info) ` If the sample flags indicate that the current sample is at least partially encrypted, this call returns relevant information about the structure of the sample data required for decryption. |
| ` int` | ` https://developer.android.com/reference/android/media/MediaExtractor#getSampleFlags()() ` Returns the current sample's flags. |
| ` long` | ` https://developer.android.com/reference/android/media/MediaExtractor#getSampleSize()() ` |
| ` long` | ` https://developer.android.com/reference/android/media/MediaExtractor#getSampleTime()() ` Returns the current sample's presentation time in microseconds. |
| ` int` | ` https://developer.android.com/reference/android/media/MediaExtractor#getSampleTrackIndex()() ` Returns the track index the current sample originates from (or -1 if no more samples are available) |
| ` int` | ` https://developer.android.com/reference/android/media/MediaExtractor#getTrackCount()() ` Count the number of tracks found in the data source. |
| ` https://developer.android.com/reference/android/media/MediaFormat` | ` https://developer.android.com/reference/android/media/MediaExtractor#getTrackFormat(int)(int index) ` Get the track format at the specified index. |
| ` boolean` | ` https://developer.android.com/reference/android/media/MediaExtractor#hasCacheReachedEndOfStream()() ` Returns true iff we are caching data and the cache has reached the end of the data stream (for now, a future seek may of course restart the fetching of data). |
| ` int` | ` https://developer.android.com/reference/android/media/MediaExtractor#readSampleData(java.nio.ByteBuffer,%20int)(https://developer.android.com/reference/java/nio/ByteBuffer byteBuf, int offset) ` Retrieve the current encoded sample and store it in the byte buffer starting at the given offset. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#release()() ` 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. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#seekTo(long,%20int)(long timeUs, int mode) ` All selected tracks seek near the requested time according to the specified mode. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#selectTrack(int)(int index) ` Subsequent calls to `https://developer.android.com/reference/android/media/MediaExtractor#readSampleData(java.nio.ByteBuffer,%20int)`, `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTrackIndex()` and `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTime()` only retrieve information for the subset of tracks selected. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(android.content.res.AssetFileDescriptor)(https://developer.android.com/reference/android/content/res/AssetFileDescriptor afd) ` Sets the data source (AssetFileDescriptor) to use. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(java.io.FileDescriptor)(https://developer.android.com/reference/java/io/FileDescriptor fd) ` Sets the data source (FileDescriptor) to use. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(android.media.MediaDataSource)(https://developer.android.com/reference/android/media/MediaDataSource dataSource) ` Sets the data source (MediaDataSource) to use. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(android.content.Context,%20android.net.Uri,%20java.util.Map<java.lang.String,java.lang.String>)(https://developer.android.com/reference/android/content/Context context, https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/java/util/Map<https://developer.android.com/reference/java/lang/String, https://developer.android.com/reference/java/lang/String> headers) ` Sets the data source as a content Uri. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(java.io.FileDescriptor,%20long,%20long)(https://developer.android.com/reference/java/io/FileDescriptor fd, long offset, long length) ` Sets the data source (FileDescriptor) to use. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(java.lang.String)(https://developer.android.com/reference/java/lang/String path) ` Sets the data source (file-path or http URL) to use. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(java.lang.String,%20java.util.Map<java.lang.String,java.lang.String>)(https://developer.android.com/reference/java/lang/String path, https://developer.android.com/reference/java/util/Map<https://developer.android.com/reference/java/lang/String, https://developer.android.com/reference/java/lang/String> headers) ` Sets the data source (file-path or http URL) to use. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setLogSessionId(android.media.metrics.LogSessionId)(https://developer.android.com/reference/android/media/metrics/LogSessionId logSessionId) ` Sets the `https://developer.android.com/reference/android/media/metrics/LogSessionId` for MediaExtractor. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#setMediaCas(android.media.MediaCas)(https://developer.android.com/reference/android/media/MediaCas mediaCas) ` *This method was deprecated in API level 33. Use the `Descrambler` system API instead, or DRM public APIs like `https://developer.android.com/reference/android/media/MediaDrm`.* |
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#unselectTrack(int)(int index) ` Subsequent calls to `https://developer.android.com/reference/android/media/MediaExtractor#readSampleData(java.nio.ByteBuffer,%20int)`, `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTrackIndex()` and `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTime()` only retrieve information for the subset of tracks selected. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/MediaExtractor#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Constants

### SAMPLE_FLAG_ENCRYPTED

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int SAMPLE_FLAG_ENCRYPTED
```

The sample is (at least partially) encrypted, see also the documentation for `https://developer.android.com/reference/android/media/MediaCodec#queueSecureInputBuffer(int,%20int,%20android.media.MediaCodec.CryptoInfo,%20long,%20int)`

<br />

Constant Value: 2 (0x00000002)

### SAMPLE_FLAG_PARTIAL_FRAME

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int SAMPLE_FLAG_PARTIAL_FRAME
```

This indicates that the buffer only contains part of a frame, and the decoder should batch the data until a buffer without this flag appears before decoding the frame.

<br />

**See also:**

- `https://developer.android.com/reference/android/media/MediaCodec#BUFFER_FLAG_PARTIAL_FRAME`

Constant Value: 4 (0x00000004)

### SAMPLE_FLAG_SYNC

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int SAMPLE_FLAG_SYNC
```

The sample is a sync sample (or in `https://developer.android.com/reference/android/media/MediaCodec`'s terminology it is a key frame.)

<br />

**See also:**

- `https://developer.android.com/reference/android/media/MediaCodec#BUFFER_FLAG_KEY_FRAME`

Constant Value: 1 (0x00000001)

### SEEK_TO_CLOSEST_SYNC

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int SEEK_TO_CLOSEST_SYNC
```

If possible, seek to the sync sample closest to the specified time

<br />

Constant Value: 2 (0x00000002)

### SEEK_TO_NEXT_SYNC

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int SEEK_TO_NEXT_SYNC
```

If possible, seek to a sync sample at or after the specified time

<br />

Constant Value: 1 (0x00000001)

### SEEK_TO_PREVIOUS_SYNC

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int SEEK_TO_PREVIOUS_SYNC
```

If possible, seek to a sync sample at or before the specified time

<br />

Constant Value: 0 (0x00000000)

## Public constructors

### MediaExtractor

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MediaExtractor ()
```

<br />

<br />

## Public methods

### advance

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean advance ()
```

Advance to the next sample. Returns false if no more sample data is available (end of stream). When extracting a local file, the behaviors of `https://developer.android.com/reference/android/media/MediaExtractor#advance()` and `https://developer.android.com/reference/android/media/MediaExtractor#readSampleData(java.nio.ByteBuffer,%20int)` are undefined in presence of concurrent writes to the same local file; more specifically, end of stream could be signalled earlier than expected.

<br />

| Returns ||
|---|---|
| `boolean` | <br /> |

### getAudioPresentations

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public List<AudioPresentation> getAudioPresentations (int trackIndex)
```

Get the list of available audio presentations for the track.

<br />

| Parameters ||
|---|---|
| `trackIndex` | `int`: index of the track. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/AudioPresentation>` | a list of available audio presentations for a given valid audio track index. The list will be empty if the source does not contain any audio presentations. This value cannot be `null`. <br /> |

### getCachedDuration

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public long getCachedDuration ()
```

Returns an estimate of how much data is presently cached in memory expressed in microseconds. Returns -1 if that information is unavailable or not applicable (no cache).

<br />

| Returns ||
|---|---|
| `long` | <br /> |

### getCasInfo

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MediaExtractor.CasInfo getCasInfo (int index)
```

Retrieves the information about the conditional access system used to scramble a track.

<br />

| Parameters ||
|---|---|
| `index` | `int`: of the track. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/MediaExtractor.CasInfo` | an `https://developer.android.com/reference/android/media/MediaExtractor.CasInfo` object describing the conditional access system. <br /> |

### getDrmInitData

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public DrmInitData getDrmInitData ()
```

Extract DRM initialization data if it exists

If the media contains a PSSH box, only PSSH version 0 is supported. The result for media with other PSSH versions is undefined.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/DrmInitData` | DRM initialization data in the content, or `null` if no recognizable DRM format is found; <br /> |

**See also:**

- `https://developer.android.com/reference/android/media/DrmInitData`

### getLogSessionId

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public LogSessionId getLogSessionId ()
```

Returns the `https://developer.android.com/reference/android/media/metrics/LogSessionId` for MediaExtractor.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/metrics/LogSessionId` | This value cannot be `null`. <br /> |

### getMetrics

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public PersistableBundle getMetrics ()
```

Return Metrics data about the current media container.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/PersistableBundle` | a `https://developer.android.com/reference/android/os/PersistableBundle` containing the set of attributes and values available for the media container being handled by this instance of MediaExtractor. The attributes are descibed in `https://developer.android.com/reference/android/media/MediaExtractor.MetricsConstants`. Additional vendor-specific fields may also be present in the return value. <br /> |

### getPsshInfo

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Map<UUID, byte[]> getPsshInfo ()
```

Get the PSSH info if present.

This method only supports version 0 PSSH boxes. The result for other versions is undefined.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/Map<https://developer.android.com/reference/java/util/UUID, byte[]>` | a map of uuid-to-bytes, with the uuid specifying the crypto scheme, and the bytes being the data specific to that scheme. This can be `null` if the source does not contain PSSH info. <br /> |

### getSampleCryptoInfo

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean getSampleCryptoInfo (MediaCodec.CryptoInfo info)
```

If the sample flags indicate that the current sample is at least partially encrypted, this call returns relevant information about the structure of the sample data required for decryption.

<br />

| Parameters ||
|---|---|
| `info` | `MediaCodec.CryptoInfo`: The android.media.MediaCodec.CryptoInfo structure to be filled in. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | true iff the sample flags contain `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_ENCRYPTED` <br /> |

### getSampleFlags

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getSampleFlags ()
```

Returns the current sample's flags.

<br />

| Returns ||
|---|---|
| `int` | Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_SYNC` - `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_ENCRYPTED` - `https://developer.android.com/reference/android/media/MediaExtractor#SAMPLE_FLAG_PARTIAL_FRAME` |

### getSampleSize

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public long getSampleSize ()
```

<br />

<br />

| Returns ||
|---|---|
| `long` | size of the current sample in bytes or -1 if no more samples are available. <br /> |

### getSampleTime

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public long getSampleTime ()
```

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

<br />

| Returns ||
|---|---|
| `long` | <br /> |

### getSampleTrackIndex

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getSampleTrackIndex ()
```

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

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### getTrackCount

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getTrackCount ()
```

Count the number of tracks found in the data source.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### getTrackFormat

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MediaFormat getTrackFormat (int index)
```

Get the track format at the specified index. More detail on the representation can be found at `https://developer.android.com/reference/android/media/MediaCodec`

The following table summarizes support for format keys across android releases:

| OS Version(s) | `MediaFormat` keys used for |||
| OS Version(s) | All Tracks | Audio Tracks | Video Tracks |
|---|---|---|---|
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#JELLY_BEAN` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_SAMPLE_RATE`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_COUNT`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_MASK`, gapless playback information^.mp3, .mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_IS_ADTS`^AAC if streaming^, codec-specific data^AAC, Vorbis^ | `https://developer.android.com/reference/android/media/MediaFormat#KEY_WIDTH`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_HEIGHT`, codec-specific data^AVC, MPEG4^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#JELLY_BEAN_MR1` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_SAMPLE_RATE`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_COUNT`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_MASK`, gapless playback information^.mp3, .mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_IS_ADTS`^AAC if streaming^, codec-specific data^AAC, Vorbis^ | `https://developer.android.com/reference/android/media/MediaFormat#KEY_WIDTH`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_HEIGHT`, codec-specific data^AVC, MPEG4^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#JELLY_BEAN_MR2` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_SAMPLE_RATE`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_COUNT`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_MASK`, gapless playback information^.mp3, .mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_IS_ADTS`^AAC if streaming^, codec-specific data^AAC, Vorbis^ | as above, plus Pixel aspect ratio information^AVC, \*^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#KITKAT` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_SAMPLE_RATE`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_COUNT`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_MASK`, gapless playback information^.mp3, .mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_IS_ADTS`^AAC if streaming^, codec-specific data^AAC, Vorbis^ | as above, plus Pixel aspect ratio information^AVC, \*^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#KITKAT_WATCH` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_SAMPLE_RATE`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_COUNT`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_CHANNEL_MASK`, gapless playback information^.mp3, .mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_IS_ADTS`^AAC if streaming^, codec-specific data^AAC, Vorbis^ | as above, plus Pixel aspect ratio information^AVC, \*^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#LOLLIPOP` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_BIT_RATE`^AAC^, codec-specific data^Opus^ | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_ROTATION`^.mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_BIT_RATE`^MPEG4^, codec-specific data^HEVC^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#LOLLIPOP_MR1` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_BIT_RATE`^AAC^, codec-specific data^Opus^ | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_ROTATION`^.mp4^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_BIT_RATE`^MPEG4^, codec-specific data^HEVC^ |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#M` | `https://developer.android.com/reference/android/media/MediaFormat#KEY_MIME`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_DURATION`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_MAX_INPUT_SIZE` | as above, plus gapless playback information^Opus^ | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_FRAME_RATE` (integer) |
| `https://developer.android.com/reference/android/os/Build.VERSION_CODES#N` | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_TRACK_ID`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_BIT_RATE`^#, .mp4^ | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_PCM_ENCODING`, `https://developer.android.com/reference/android/media/MediaFormat#KEY_PROFILE`^AAC^ | as above, plus `https://developer.android.com/reference/android/media/MediaFormat#KEY_HDR_STATIC_INFO`^#, .webm^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_COLOR_STANDARD`^#^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_COLOR_TRANSFER`^#^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_COLOR_RANGE`^#^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_PROFILE`^MPEG2, H.263, MPEG4, AVC, HEVC, VP9^, `https://developer.android.com/reference/android/media/MediaFormat#KEY_LEVEL`^H.263, MPEG4, AVC, HEVC, VP9^, codec-specific data^VP9^ |
| **Notes:** #: container-specified value only. .mp4, .webm...: for listed containers MPEG4, AAC...: for listed codecs ||||
| Note that that level information contained in the container many times does not match the level of the actual bitstream. You may want to clear the level using `MediaFormat.setString(KEY_LEVEL, null)` before using the track format to find a decoder that can play back a particular track. ||||
| **\*Pixel (sample) aspect ratio** is returned in the following keys. The display width can be calculated for example as: display-width = display-height \* crop-width / crop-height \* sar-width / sar-height ||||
| `"sar-width"` | Integer | Pixel aspect ratio width ||
| `"sar-height"` | Integer | Pixel aspect ratio height ||

<br />

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat` | <br /> |

### hasCacheReachedEndOfStream

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean hasCacheReachedEndOfStream ()
```

Returns true iff we are caching data and the cache has reached the end of the data stream (for now, a future seek may of course restart the fetching of data). This API only returns a meaningful result if `https://developer.android.com/reference/android/media/MediaExtractor#getCachedDuration()` indicates the presence of a cache, i.e. does NOT return -1.

<br />

| Returns ||
|---|---|
| `boolean` | <br /> |

### readSampleData

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int readSampleData (ByteBuffer byteBuf, 
                int offset)
```

Retrieve the current encoded sample and store it in the byte buffer starting at the given offset.

**Note:** As of API 21, on success the position and limit of `byteBuf` is updated to point to the data just read.

<br />

| Parameters ||
|---|---|
| `byteBuf` | `ByteBuffer`: the destination byte buffer. This value cannot be `null`. <br /> |
| `offset` | `int` <br /> |

| Returns ||
|---|---|
| `int` | the sample size (or -1 if no more samples are available). <br /> |

### release

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void release ()
```

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.

<br />

### seekTo

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void seekTo (long timeUs, 
                int mode)
```

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

<br />

| Parameters ||
|---|---|
| `timeUs` | `long` <br /> |
| `mode` | `int`: Value is one of the following: - `https://developer.android.com/reference/android/media/MediaExtractor#SEEK_TO_PREVIOUS_SYNC` - `https://developer.android.com/reference/android/media/MediaExtractor#SEEK_TO_NEXT_SYNC` - `https://developer.android.com/reference/android/media/MediaExtractor#SEEK_TO_CLOSEST_SYNC` |

### selectTrack

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void selectTrack (int index)
```

Subsequent calls to `https://developer.android.com/reference/android/media/MediaExtractor#readSampleData(java.nio.ByteBuffer,%20int)`, `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTrackIndex()` and `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTime()` only retrieve information for the subset of tracks selected. Selecting the same track multiple times has no effect, the track is only selected once.

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |

### setDataSource

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (AssetFileDescriptor afd)
```

Sets the data source (AssetFileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

<br />

| Parameters ||
|---|---|
| `afd` | `AssetFileDescriptor`: the AssetFileDescriptor for the file you want to extract from. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` |   |
| `https://developer.android.com/reference/java/lang/IllegalStateException` |   |

### setDataSource

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (FileDescriptor fd)
```

Sets the data source (FileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

<br />

| Parameters ||
|---|---|
| `fd` | `FileDescriptor`: the FileDescriptor for the file you want to extract from. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### setDataSource

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (MediaDataSource dataSource)
```

Sets the data source (MediaDataSource) to use.

<br />

| Parameters ||
|---|---|
| `dataSource` | `MediaDataSource`: the MediaDataSource for the media you want to extract from. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if dataSource is invalid. |
| `https://developer.android.com/reference/java/io/IOException` |   |

### setDataSource

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (Context context, 
                Uri uri, 
                Map<String, String> headers)
```

Sets the data source as a content Uri.

<br />

| Parameters ||
|---|---|
| `context` | `Context`: the Context to use when resolving the Uri. This value cannot be `null`. <br /> |
| `uri` | `Uri`: the Content URI of the data you want to extract from. When `uri` refers to a network file the `https://developer.android.com/reference/android/Manifest.permission#INTERNET` permission is required. This value cannot be `null`. |
| `headers` | `Map`: the headers to be sent together with the request for the data. This can be `null` if no specific headers are to be sent with the request. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### setDataSource

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (FileDescriptor fd, 
                long offset, 
                long length)
```

Sets the data source (FileDescriptor) to use. The FileDescriptor must be seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

<br />

| Parameters ||
|---|---|
| `fd` | `FileDescriptor`: the FileDescriptor for the file you want to extract from. This value cannot be `null`. <br /> |
| `offset` | `long`: the offset into the file where the data to be extracted starts, in bytes <br /> |
| `length` | `long`: the length in bytes of the data to be extracted <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### setDataSource

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (String path)
```

Sets the data source (file-path or http URL) to use.

<br />

| Parameters ||
|---|---|
| `path` | `String`: the path of the file, or the http URL of the stream When `path` refers to a local file, the file may actually be opened by a process other than the calling application. This implies that the pathname should be an absolute path (as any other process runs with unspecified current working directory), and that the pathname should reference a world-readable file. As an alternative, the application could first open the file for reading, and then use the file descriptor form `https://developer.android.com/reference/android/media/MediaExtractor#setDataSource(java.io.FileDescriptor)`. When `path` refers to a network file the `https://developer.android.com/reference/android/Manifest.permission#INTERNET` permission is required. This value cannot be `null`. |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### setDataSource

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSource (String path, 
                Map<String, String> headers)
```

Sets the data source (file-path or http URL) to use.

<br />

| Parameters ||
|---|---|
| `path` | `String`: the path of the file, or the http URL When `path` refers to a network file the `https://developer.android.com/reference/android/Manifest.permission#INTERNET` permission is required. This value cannot be `null`. |
| `headers` | `Map`: the headers associated with the http request for the stream you want to play. This can be `null` if no specific headers are to be sent with the request. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### setLogSessionId

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setLogSessionId (LogSessionId logSessionId)
```

Sets the `https://developer.android.com/reference/android/media/metrics/LogSessionId` for MediaExtractor.

<br />

| Parameters ||
|---|---|
| `logSessionId` | `LogSessionId`: This value cannot be `null`. <br /> |

### setMediaCas

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setMediaCas (MediaCas mediaCas)
```

<br />

**This method was deprecated in API level 33.**   

Use the `Descrambler` system API instead, or DRM public APIs like `https://developer.android.com/reference/android/media/MediaDrm`.

Sets the MediaCas instance to use. This should be called after a successful setDataSource() if at least one track reports mime type of `https://developer.android.com/reference/android/media/MediaFormat#MIMETYPE_AUDIO_SCRAMBLED` or `https://developer.android.com/reference/android/media/MediaFormat#MIMETYPE_VIDEO_SCRAMBLED`. Stream parsing will not proceed until a valid MediaCas object is provided.

<br />

| Parameters ||
|---|---|
| `mediaCas` | `MediaCas`: the MediaCas object to use. This value cannot be `null`. <br /> |

### unselectTrack

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void unselectTrack (int index)
```

Subsequent calls to `https://developer.android.com/reference/android/media/MediaExtractor#readSampleData(java.nio.ByteBuffer,%20int)`, `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTrackIndex()` and `https://developer.android.com/reference/android/media/MediaExtractor#getSampleTime()` only retrieve information for the subset of tracks selected.

<br />

| Parameters ||
|---|---|
| `index` | `int` <br /> |

## Protected methods

### finalize

Added in [API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void finalize ()
```

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the `finalize` method to dispose of system resources or to perform other cleanup.

The general contract of `finalize` is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The `finalize` method may take any action, including making this object available again to other threads; the usual purpose of `finalize`, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The `finalize` method of class `Object` performs no special action; it simply returns normally. Subclasses of `Object` may override this definition.

The Java programming language does not guarantee which thread will invoke the `finalize` method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the `finalize` method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The `finalize` method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the `finalize` method causes the finalization of this object to be halted, but is otherwise ignored.

<br />