public final class MediaExtractor extends Object
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;
Modifier and Type | Class and Description |
---|---|
static interface |
MediaExtractor.SampleFlag |
static interface |
MediaExtractor.SeekMode |
Modifier and Type | Field and Description |
---|---|
static int |
SAMPLE_FLAG_ENCRYPTED
The sample is (at least partially) encrypted, see also the documentation
for
MediaCodec.queueSecureInputBuffer(int, int, android.media.MediaCodec.CryptoInfo, long, int) |
static int |
SAMPLE_FLAG_SYNC
The sample is a sync sample (or in
MediaCodec 's terminology
it is a key frame.) |
static int |
SEEK_TO_CLOSEST_SYNC
If possible, seek to the sync sample closest to the specified time
|
static int |
SEEK_TO_NEXT_SYNC
If possible, seek to a sync sample at or after the specified time
|
static int |
SEEK_TO_PREVIOUS_SYNC
If possible, seek to a sync sample at or before the specified time
|
Constructor and Description |
---|
MediaExtractor() |
Modifier and Type | Method and Description |
---|---|
boolean |
advance()
Advance to the next sample.
|
protected void |
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
long |
getCachedDuration()
Returns an estimate of how much data is presently cached in memory
expressed in microseconds.
|
DrmInitData |
getDrmInitData()
Extract DRM initialization data if it exists
|
Map<UUID,byte[]> |
getPsshInfo()
Get the PSSH info if present.
|
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.
|
int |
getSampleFlags()
Returns the current sample's flags.
|
long |
getSampleTime()
Returns the current sample's presentation time in microseconds.
|
int |
getSampleTrackIndex()
Returns the track index the current sample originates from (or -1
if no more samples are available)
|
int |
getTrackCount()
Count the number of tracks found in the data source.
|
MediaFormat |
getTrackFormat(int index)
Get the track format at the specified index.
|
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).
|
int |
readSampleData(ByteBuffer byteBuf,
int offset)
Retrieve the current encoded sample and store it in the byte buffer
starting at the given offset.
|
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.
|
void |
seekTo(long timeUs,
int mode)
All selected tracks seek near the requested time according to the
specified mode.
|
void |
selectTrack(int index)
Subsequent calls to
readSampleData(java.nio.ByteBuffer, int) , getSampleTrackIndex() and
getSampleTime() only retrieve information for the subset of tracks
selected. |
void |
setDataSource(AssetFileDescriptor afd)
Sets the data source (AssetFileDescriptor) to use.
|
void |
setDataSource(Context context,
Uri uri,
Map<String,String> headers)
Sets the data source as a content Uri.
|
void |
setDataSource(FileDescriptor fd)
Sets the data source (FileDescriptor) to use.
|
void |
setDataSource(FileDescriptor fd,
long offset,
long length)
Sets the data source (FileDescriptor) to use.
|
void |
setDataSource(MediaDataSource dataSource)
Sets the data source (MediaDataSource) to use.
|
void |
setDataSource(String path)
Sets the data source (file-path or http URL) to use.
|
void |
setDataSource(String path,
Map<String,String> headers)
Sets the data source (file-path or http URL) to use.
|
void |
unselectTrack(int index)
Subsequent calls to
readSampleData(java.nio.ByteBuffer, int) , getSampleTrackIndex() and
getSampleTime() only retrieve information for the subset of tracks
selected. |
public static final int SEEK_TO_PREVIOUS_SYNC
public static final int SEEK_TO_NEXT_SYNC
public static final int SEEK_TO_CLOSEST_SYNC
public static final int SAMPLE_FLAG_SYNC
MediaCodec
's terminology
it is a key frame.)public static final int SAMPLE_FLAG_ENCRYPTED
MediaCodec.queueSecureInputBuffer(int, int, android.media.MediaCodec.CryptoInfo, long, int)
public final void setDataSource(MediaDataSource dataSource) throws IOException
dataSource
- the MediaDataSource for the media you want to extract fromIllegalArgumentException
- if dataSource is invalid.IOException
public final void setDataSource(Context context, Uri uri, Map<String,String> headers) throws IOException
context
- the Context to use when resolving the Uriuri
- the Content URI of the data you want to extract from.headers
- 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.IOException
public final void setDataSource(String path, Map<String,String> headers) throws IOException
path
- the path of the file, or the http URLheaders
- 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.IOException
public final void setDataSource(String path) throws IOException
path
- 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 setDataSource(FileDescriptor)
.
IOException
public final void setDataSource(AssetFileDescriptor afd) throws IOException, IllegalArgumentException, IllegalStateException
afd
- the AssetFileDescriptor for the file you want to extract from.IOException
IllegalArgumentException
IllegalStateException
public final void setDataSource(FileDescriptor fd) throws IOException
fd
- the FileDescriptor for the file you want to extract from.IOException
public final void setDataSource(FileDescriptor fd, long offset, long length) throws IOException
fd
- the FileDescriptor for the file you want to extract from.offset
- the offset into the file where the data to be extracted starts, in byteslength
- the length in bytes of the data to be extractedIOException
protected void finalize()
Object
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 JavaTM 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.
public final void release()
public final int getTrackCount()
public DrmInitData getDrmInitData()
null
if no recognizable DRM format is found;DrmInitData
public Map<UUID,byte[]> getPsshInfo()
null
if the source does not contain PSSH info.public MediaFormat getTrackFormat(int index)
MediaCodec
The following table summarizes support for format keys across android releases:
OS Version(s) | MediaFormat keys used for
| ||
---|---|---|---|
All Tracks | Audio Tracks | Video Tracks | |
Build.VERSION_CODES.JELLY_BEAN |
MediaFormat.KEY_MIME ,MediaFormat.KEY_DURATION ,MediaFormat.KEY_MAX_INPUT_SIZE |
MediaFormat.KEY_SAMPLE_RATE ,MediaFormat.KEY_CHANNEL_COUNT ,MediaFormat.KEY_CHANNEL_MASK ,gapless playback information.mp3, .mp4, MediaFormat.KEY_IS_ADTS AAC if streaming,codec-specific dataAAC, Vorbis |
MediaFormat.KEY_WIDTH ,MediaFormat.KEY_HEIGHT ,codec-specific dataAVC, MPEG4 |
Build.VERSION_CODES.JELLY_BEAN_MR1 |
|||
Build.VERSION_CODES.JELLY_BEAN_MR2 |
as above, plus Pixel aspect ratio informationAVC, * |
||
Build.VERSION_CODES.KITKAT |
|||
Build.VERSION_CODES.KITKAT_WATCH |
|||
Build.VERSION_CODES.LOLLIPOP |
as above, plusMediaFormat.KEY_BIT_RATE AAC,codec-specific dataOpus |
as above, plusMediaFormat.KEY_ROTATION .mp4,MediaFormat.KEY_BIT_RATE MPEG4,codec-specific dataHEVC |
|
Build.VERSION_CODES.LOLLIPOP_MR1 |
|||
Build.VERSION_CODES.M |
as above, plus gapless playback informationOpus |
as above, plusMediaFormat.KEY_FRAME_RATE (integer) |
|
Build.VERSION_CODES.N |
as above, plusMediaFormat.KEY_TRACK_ID ,MediaFormat.KEY_BIT_RATE #, .mp4 |
as above, plusMediaFormat.KEY_PCM_ENCODING ,MediaFormat.KEY_PROFILE AAC |
as above, plusMediaFormat.KEY_HDR_STATIC_INFO #, .webm,MediaFormat.KEY_COLOR_STANDARD #,MediaFormat.KEY_COLOR_TRANSFER #,MediaFormat.KEY_COLOR_RANGE #,MediaFormat.KEY_PROFILE MPEG2, H.263, MPEG4, AVC, HEVC, VP9,MediaFormat.KEY_LEVEL H.263, MPEG4, AVC, HEVC, VP9,codec-specific dataVP9 |
Notes: |
|||
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
|
|||
*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 |
|||
Format Key | Value Type | Description | |
"sar-width" | Integer | Pixel aspect ratio width | |
"sar-height" | Integer | Pixel aspect ratio height |
public void selectTrack(int index)
readSampleData(java.nio.ByteBuffer, int)
, getSampleTrackIndex()
and
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.public void unselectTrack(int index)
readSampleData(java.nio.ByteBuffer, int)
, getSampleTrackIndex()
and
getSampleTime()
only retrieve information for the subset of tracks
selected.public void seekTo(long timeUs, int mode)
public boolean advance()
advance()
and
readSampleData(java.nio.ByteBuffer, int)
are undefined in presence of concurrent
writes to the same local file; more specifically, end of stream
could be signalled earlier than expected.public int readSampleData(ByteBuffer byteBuf, int offset)
Note:As of API 21, on success the position and limit of
byteBuf
is updated to point to the data just read.
byteBuf
- the destination byte bufferpublic int getSampleTrackIndex()
public long getSampleTime()
public int getSampleFlags()
public boolean getSampleCryptoInfo(MediaCodec.CryptoInfo info)
info
- The android.media.MediaCodec.CryptoInfo structure
to be filled in.SAMPLE_FLAG_ENCRYPTED
public long getCachedDuration()
public boolean hasCacheReachedEndOfStream()
getCachedDuration()
indicates the presence of a cache, i.e. does NOT return -1.