Using Mediaextractor to Read Mp3 Files Android
Course Overview
MediaExtractor facilitates extraction of demuxed, typically encoded, media data from a data source.
Information technology 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.accelerate(); } extractor.release(); extractor = nix; Summary
| Constants | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| int | SAMPLE_FLAG_ENCRYPTED | The sample is (at least partially) encrypted, come across too 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 fourth dimension | |||||||||
| int | SEEK_TO_PREVIOUS_SYNC | If possible, seek to a sync sample at or before the specified time | |||||||||
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| | | ||||||||||
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| | Accelerate to the next sample. | ||||||||||
| | Returns an estimate of how much information is presently cached in retentiveness expressed in microseconds. | ||||||||||
| | Become the PSSH info if present. | ||||||||||
| | If the sample flags indicate that the current sample is at to the lowest degree partially encrypted, this call returns relevant information most the construction of the sample information required for decryption. | ||||||||||
| | Returns the current sample's flags. | ||||||||||
| | Returns the current sample's presentation time in microseconds. | ||||||||||
| | Returns the track alphabetize the current sample originates from (or -1 if no more samples are bachelor) | ||||||||||
| | 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 stop of the data stream (for now, a future seek may of course restart the fetching of data). | ||||||||||
| | Retrieve the electric current encoded sample and store it in the byte buffer starting at the given offset. | ||||||||||
| | Make certain you lot call this when you're done to free upwards any resources instead of relying on the garbage collector to do this for y'all at some point in the future. | ||||||||||
| | All selected tracks seek about the requested fourth dimension according to the specified mode. | ||||||||||
| | Subsequent calls to | ||||||||||
| | Sets the data source (file-path or http URL) to utilize. | ||||||||||
| | Sets the data source (file-path or http URL) to use. | ||||||||||
| | Sets the data source (FileDescriptor) to apply. | ||||||||||
| | Sets the data source (FileDescriptor) to use. | ||||||||||
| | Sets the data source equally a content Uri. | ||||||||||
| | Subsequent calls to | ||||||||||
| Protected Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| | Invoked when the garbage collector has detected that this instance is no longer reachable. | ||||||||||
| [Aggrandize] Inherited Methods | |||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
| |||||||||||||||||||||||||||||||||
Constants
public static concluding int SAMPLE_FLAG_ENCRYPTED
Constant Value: two (0x00000002)
public static final int SAMPLE_FLAG_SYNC
The sample is a sync sample
Constant Value: 1 (0x00000001)
public static concluding int SEEK_TO_CLOSEST_SYNC
If possible, seek to the sync sample closest to the specified time
Constant Value: two (0x00000002)
public static fina 50 int SEEK_TO_NEXT_SYNC
If possible, seek to a sync sample at or after the specified time
Constant Value: 1 (0x00000001)
public static final int SEEK_TO_PREVIOUS_SYNC
If possible, seek to a sync sample at or before the specified time
Constant Value: 0 (0x00000000)
Public Constructors
public MediaExtractor ()
Public Methods
public boolean advance ()
Advance to the next sample. Returns simulated if no more sample information is available (end of stream).
public long getCachedDuration ()
Returns an estimate of how much data is presently buried in retentivity expressed in microseconds. Returns -i if that information is unavailable or not applicable (no cache).
public Map<UUID, byte[]> getPsshInfo ()
Get the PSSH info if present.
Returns
- a map of uuid-to-bytes, with the uuid specifying the crypto scheme, and the bytes being the information specific to that scheme.
public boolean getSampleCryptoInfo (MediaCodec.CryptoInfo info)
If the sample flags indicate that the electric current sample is at least partially encrypted, this call returns relevant information virtually the structure of the sample data required for decryption.
Parameters
| info | The android.media.MediaCodec.CryptoInfo construction to be filled in. |
|---|
public int getSampleFlags ()
Returns the electric current sample's flags.
public long getSampleTime ()
Returns the current sample'southward presentation time in microseconds. or -1 if no more samples are available.
public int getSampleTrackIndex ()
Returns the rails index t he current sample originates from (or -one if no more samples are bachelor)
public terminal int getTrackCount ()
Count the number of tracks found in the information source.
public MediaFormat getTrackFormat (int alphabetize)
Become the track format at the specified index. More detail on the representation can be establish at MediaCodec
public boolean hasCacheReachedEndOfStream ()
Returns true iff we are caching information and the cache has reached the end of the data stream (for now, a time to come seek may of course restart the fetching of data). This API only returns a meaningful outcome if getCachedDuration() indicates the presence of a enshroud, i.due east. does NOT return -one.
public int readSampleData (ByteBuffer byteBuf, int offset)
Call back the electric current encoded sample and store it in the byte buffer starting at the given showtime. Returns the sample size (or -1 if no more samples are available).
public final void release ()
Make certain you call this when y'all're washed to gratuitous upward any resources instead of relying on the garbage collector to practise this for you at some point in the futurity.
public void seekTo (long timeUs, int mode)
All selected tracks seek near the requested time according to the specified mode.
public void selectTrack (int index)
public final void setDataSource (String path)
Sets the information source (file-path or http URL) to use.
Parameters
| path | the path of the file, or the http URL of the stream When |
|---|
public terminal void setDataSource (Cord path, Map<Cord, Cord> headers)
Sets the information source (file-path or http URL) to utilise.
Parameters
| path | the path of the file, or the http URL |
|---|---|
| headers | the headers associated with the http request for the stream yous desire to play |
public final void setDataSource (FileDescriptor fd)
Sets the data source (FileDescriptor) to utilize. It is the caller's responsibility to shut the file descriptor. It is safe to practise and then as presently equally this call returns.
Parameters
| fd | the FileDescriptor for the file you want to extract from. |
|---|
public final 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). Information technology is the caller'south responsibleness to close the file descriptor. It is condom to exercise so equally shortly as this call returns.
Parameters
| fd | the FileDescriptor for the file you desire to extract from. |
|---|---|
| offset | the offset into the file where the data to exist extracted starts, in bytes |
| length | the length in bytes of the data to be extracted |
public final void setDataSource (Context context, Uri uri, Map<String, String> headers)
Sets the data source equally a content Uri.
Parameters
| context | the Context to use when resolving the Uri |
|---|---|
| uri | the Content URI of the data yous desire to extract from. |
| headers | the headers to be sent together with the request for the data |
public void unselectTrack (int index)
Protected Methods
protected void finalize ()
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 exist run a long time after the object is no longer reachable, depending on retention pressure, and so information technology's a bad thought to rely on them for cleanup. Note likewise that finalizers are run on a unmarried VM-wide finalizer thread, then 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 telephone call a native method to destroy that peer. Fifty-fifty then, it'southward amend 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 bargain 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. Y'all are responsible for calling super.finalize() yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Particular 7, "Avoid finalizers" for more than.
Source: https://spot.pcc.edu/~mgoodman/developer.android.com/reference/android/media/MediaExtractor.html
From class java.lang.Object
0 Response to "Using Mediaextractor to Read Mp3 Files Android"
Post a Comment