MediaParser Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Parses media container formats and extracts contained media samples and metadata.
[Android.Runtime.Register("android/media/MediaParser", ApiSince=30, DoNotGenerateAcw=true)]
public sealed class MediaParser : Java.Lang.Object
[<Android.Runtime.Register("android/media/MediaParser", ApiSince=30, DoNotGenerateAcw=true)>]
type MediaParser = class
inherit Object
- Inheritance
- Attributes
Remarks
Parses media container formats and extracts contained media samples and metadata.
This class provides access to a battery of low-level media container parsers. Each instance of this class is associated to a specific media parser implementation which is suitable for extraction from a specific media container format. The media parser implementation assignment depends on the factory method (see #create
and #createByName
) used to create the instance.
Users must implement the following to use this class.
<ul> <li>InputReader
: Provides the media container's bytes to parse. <li>OutputConsumer
: Provides a sink for all extracted data and metadata. </ul>
The following code snippet includes a usage example:
MyOutputConsumer myOutputConsumer = new MyOutputConsumer();
MyInputReader myInputReader = new MyInputReader("www.example.com");
MediaParser mediaParser = MediaParser.create(myOutputConsumer);
while (mediaParser.advance(myInputReader)) {}
mediaParser.release();
mediaParser = null;
The following code snippet provides a rudimentary OutputConsumer
sample implementation which extracts and publishes all video samples:
class VideoOutputConsumer implements MediaParser.OutputConsumer {
private byte[] sampleDataBuffer = new byte[4096];
private byte[] discardedDataBuffer = new byte[4096];
private int videoTrackIndex = -1;
private int bytesWrittenCount = 0;
@Override
public void onSeekMapFound(int i, @NonNull MediaFormat mediaFormat) {
// Do nothing.
}
@Override
public void onTrackDataFound(int i, @NonNull TrackData trackData) {
MediaFormat mediaFormat = trackData.mediaFormat;
if (videoTrackIndex == -1 &&
mediaFormat
.getString(MediaFormat.KEY_MIME, /* defaultValue= */ "")
.startsWith("video/")) {
videoTrackIndex = i;
}
}
@Override
public void onSampleDataFound(int trackIndex, @NonNull InputReader inputReader)
throws IOException {
int numberOfBytesToRead = (int) inputReader.getLength();
if (videoTrackIndex != trackIndex) {
// Discard contents.
inputReader.read(
discardedDataBuffer,
/* offset= */ 0,
Math.min(discardDataBuffer.length, numberOfBytesToRead));
} else {
ensureSpaceInBuffer(numberOfBytesToRead);
int bytesRead = inputReader.read(
sampleDataBuffer, bytesWrittenCount, numberOfBytesToRead);
bytesWrittenCount += bytesRead;
}
}
@Override
public void onSampleCompleted(
int trackIndex,
long timeMicros,
int flags,
int size,
int offset,
@Nullable CryptoInfo cryptoData) {
if (videoTrackIndex != trackIndex) {
return; // It's not the video track. Ignore.
}
byte[] sampleData = new byte[size];
int sampleStartOffset = bytesWrittenCount - size - offset;
System.arraycopy(
sampleDataBuffer,
sampleStartOffset,
sampleData,
/* destPos= */ 0,
size);
// Place trailing bytes at the start of the buffer.
System.arraycopy(
sampleDataBuffer,
bytesWrittenCount - offset,
sampleDataBuffer,
/* destPos= */ 0,
/* size= */ offset);
bytesWrittenCount = bytesWrittenCount - offset;
publishSample(sampleData, timeMicros, flags);
}
private void ensureSpaceInBuffer(int numberOfBytesToRead) {
int requiredLength = bytesWrittenCount + numberOfBytesToRead;
if (requiredLength > sampleDataBuffer.length) {
sampleDataBuffer = Arrays.copyOf(sampleDataBuffer, requiredLength);
}
}
}
Java documentation for android.media.MediaParser
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Fields
ParameterAdtsEnableCbrSeeking |
Sets whether constant bitrate seeking should be enabled for ADTS parsing. |
ParameterAmrEnableCbrSeeking |
Sets whether constant bitrate seeking should be enabled for AMR. |
ParameterFlacDisableId3 |
Sets whether the ID3 track should be disabled for FLAC. |
ParameterMatroskaDisableCuesSeeking |
Sets whether Matroska parsing should avoid seeking to the cues element. |
ParameterMp3DisableId3 |
Sets whether the ID3 track should be disabled for MP3. |
ParameterMp3EnableCbrSeeking |
Sets whether constant bitrate seeking should be enabled for MP3. |
ParameterMp3EnableIndexSeeking |
Sets whether MP3 parsing should generate a time-to-byte mapping. |
ParameterMp4IgnoreEditLists |
Sets whether MP4 parsing should ignore edit lists. |
ParameterMp4IgnoreTfdtBox |
Sets whether MP4 parsing should ignore the tfdt box. |
ParameterMp4TreatVideoFramesAsKeyframes |
Sets whether MP4 parsing should treat all video frames as key frames. |
ParameterTsAllowNonIdrAvcKeyframes |
Sets whether TS should treat samples consisting of non-IDR I slices as synchronization samples (key-frames). |
ParameterTsDetectAccessUnits |
Sets whether TS parsing should split AVC stream into access units based on slice headers. |
ParameterTsEnableHdmvDtsAudioStreams |
Sets whether TS parsing should handle HDMV DTS audio streams. |
ParameterTsIgnoreAacStream |
Sets whether TS parsing should ignore AAC elementary streams. |
ParameterTsIgnoreAvcStream |
Sets whether TS parsing should ignore AVC elementary streams. |
ParameterTsIgnoreSpliceInfoStream |
Sets whether TS parsing should ignore splice information streams. |
ParameterTsMode |
Sets the operation mode for TS parsing. |
ParserNameAc3 |
Parser for the AC-3 container format, as defined in Digital Audio Compression Standard (AC-3). |
ParserNameAc4 |
Parser for the AC-4 container format, as defined by Dolby AC-4: Audio delivery for Next-Generation Entertainment Services. |
ParserNameAdts |
Parser for the ADTS container format, as defined in ISO/IEC 13818-7. |
ParserNameAmr |
Parser for the AMR container format, as defined in RFC 4867. |
ParserNameFlac |
Parser for the FLAC container format, as defined in the spec. |
ParserNameFlv |
Parser for the FLV container format, as defined in Adobe Flash Video File Format Specification. |
ParserNameFmp4 |
Parser for fragmented files using the MP4 container format, as defined in ISO/IEC 14496-12. |
ParserNameMatroska |
Parser for the Matroska container format, as defined in the spec. |
ParserNameMp3 |
Parser for the MP3 container format, as defined in ISO/IEC 11172-3. |
ParserNameMp4 |
Parser for non-fragmented files using the MP4 container format, as defined in ISO/IEC 14496-12. |
ParserNameOgg |
Parser for the OGG container format, as defined in RFC 3533. |
ParserNamePs |
Parser for the PS container format, as defined in ISO/IEC 11172-1. |
ParserNameTs |
Parser for the TS container format, as defined in ISO/IEC 13818-1. |
ParserNameUnknown |
Parser name returned by |
ParserNameWav |
Parser for the WAV container format, as defined in Multimedia Programming Interface and Data Specifications. |
SampleFlagDecodeOnly |
Obsolete.
Indicates that the sample should be decoded but not rendered. |
SampleFlagEncrypted |
Obsolete.
Indicates that the sample is (at least partially) encrypted. |
SampleFlagHasSupplementalData |
Obsolete.
Indicates that the sample has supplemental data. |
SampleFlagKeyFrame |
Obsolete.
Indicates that the sample holds a synchronization sample. |
SampleFlagLastSample |
Obsolete.
Indicates that the sample is known to contain the last media sample of the stream. |
Properties
Class |
Returns the runtime class of this |
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
LogSessionId | |
ParserName |
Returns the name of the backing parser implementation. |
PeerReference | (Inherited from Object) |
ThresholdClass |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. (Inherited from Object) |
ThresholdType |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. (Inherited from Object) |
Methods
Advance(MediaParser+ISeekableInputReader) |
Makes progress in the extraction of the input media stream, unless the end of the input has been reached. |
Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
Create(MediaParser+IOutputConsumer, String[]) |
Creates an instance whose backing parser will be selected by sniffing the content during the
first |
CreateByName(String, MediaParser+IOutputConsumer) |
Creates an instance backed by the parser with the given |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
GetParserNames(MediaFormat) |
Returns an immutable list with the names of the parsers that are suitable for container
formats with the given |
JavaFinalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
Release() |
Releases any acquired resources. |
Seek(MediaParser+SeekPoint) |
Seeks within the media container being extracted. |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
SetParameter(String, Object) |
Sets parser-specific parameters which allow customizing behavior. |
SupportsParameter(String) |
Returns whether the given |
ToArray<T>() | (Inherited from Object) |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
UnregisterFromRuntime() | (Inherited from Object) |
Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
IJavaPeerable.Disposed() | (Inherited from Object) |
IJavaPeerable.DisposeUnlessReferenced() | (Inherited from Object) |
IJavaPeerable.Finalized() | (Inherited from Object) |
IJavaPeerable.JniManagedPeerState | (Inherited from Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) | (Inherited from Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) | (Inherited from Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) | (Inherited from Object) |
Extension Methods
JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
JavaCast<TResult>(IJavaObject) | |
GetJniTypeName(IJavaPeerable) |
Gets the JNI name of the type of the instance |
JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |