java.lang.Object | |
↳ | android.media.MediaExtractor |
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;
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | SAMPLE_FLAG_ENCRYPTED |
The sample is (at least partially) encrypted, see also the documentation
for
queueSecureInputBuffer(int, int, MediaCodec.CryptoInfo, long, int)
|
|||||||||
int | SAMPLE_FLAG_SYNC | The sample is a sync sample | |||||||||
int | SEEK_TO_CLOSEST_SYNC | If possible, seek to the sync sample closest to the specified time | |||||||||
int | SEEK_TO_NEXT_SYNC | If possible, seek to a sync sample at or after the specified time | |||||||||
int | SEEK_TO_PREVIOUS_SYNC | If possible, seek to a sync sample at or before the specified time |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Advance to the next sample.
|
||||||||||
|
Returns an estimate of how much data is presently cached in memory
expressed in microseconds.
|
||||||||||
|
Get the PSSH info if present.
|
||||||||||
|
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.
|
||||||||||
|
Returns the current sample's flags.
|
||||||||||
|
Returns the current sample's presentation time in microseconds.
|
||||||||||
|
Returns the track index the current sample originates from (or -1
if no more samples are available)
|
||||||||||
|
Count the number of tracks found in the data source.
|
||||||||||
|
Get the track format at the specified index.
|
||||||||||
|
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).
|
||||||||||
|
Retrieve the current encoded sample and store it in the byte buffer
starting at the given offset.
|
||||||||||
|
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.
|
||||||||||
|
All selected tracks seek near the requested time according to the
specified mode.
|
||||||||||
|
Subsequent calls to
readSampleData(ByteBuffer, int)
,
getSampleTrackIndex()
and
getSampleTime()
only retrieve information for the subset of tracks
selected.
|
||||||||||
|
Sets the data source (file-path or http URL) to use.
|
||||||||||
|
Sets the data source (file-path or http URL) to use.
|
||||||||||
|
Sets the data source (FileDescriptor) to use.
|
||||||||||
|
Sets the data source (FileDescriptor) to use.
|
||||||||||
|
Sets the data source as a content Uri.
|
||||||||||
|
Subsequent calls to
readSampleData(ByteBuffer, int)
,
getSampleTrackIndex()
and
getSampleTime()
only retrieve information for the subset of tracks
selected.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods
|
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
The sample is (at least partially) encrypted, see also the documentation
for
queueSecureInputBuffer(int, int, MediaCodec.CryptoInfo, long, int)
The sample is a sync sample
If possible, seek to the sync sample closest to the specified time
If possible, seek to a sync sample at or after the specified time
If possible, seek to a sync sample at or before the specified time
Advance to the next sample. Returns false if no more sample data is available (end of stream).
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).
Get the PSSH info if present.
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.
info | The android.media.MediaCodec.CryptoInfo structure to be filled in. |
---|
SAMPLE_FLAG_ENCRYPTED
Returns the current sample's presentation time in microseconds. or -1 if no more samples are available.
Returns the track index the current sample originates from (or -1 if no more samples are available)
Count the number of tracks found in the data source.
Get the track format at the specified index.
More detail on the representation can be found at
MediaCodec
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
getCachedDuration()
indicates the presence of a cache, i.e. does NOT return -1.
Retrieve the current encoded sample and store it in the byte buffer starting at the given offset. Returns the sample size (or -1 if no more samples are available).
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.
All selected tracks seek near the requested time according to the specified mode.
Subsequent calls to
readSampleData(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.
Sets the data source (file-path or http URL) to use.
path |
the path of the file, or the http URL of the stream
When
|
---|
IOException |
---|
Sets the data source (file-path or http URL) to use.
path | the path of the file, or the http URL |
---|---|
headers | the headers associated with the http request for the stream you want to play |
IOException |
---|
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.
fd | the FileDescriptor for the file you want to extract from. |
---|
IOException |
---|
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.
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 bytes |
length | the length in bytes of the data to be extracted |
IOException |
---|
Sets the data source as a content Uri.
context | the Context to use when resolving the Uri |
---|---|
uri | the Content URI of the data you want to extract from. |
headers | the headers to be sent together with the request for the data |
IOException |
---|
Subsequent calls to
readSampleData(ByteBuffer, int)
,
getSampleTrackIndex()
and
getSampleTime()
only retrieve information for the subset of tracks
selected.
Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.
Note that objects that override
finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit
close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a
BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you
must
use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling
super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.