Share via


MediaMuxer Class

Definition

MediaMuxer facilitates muxing elementary streams.

[Android.Runtime.Register("android/media/MediaMuxer", DoNotGenerateAcw=true)]
public sealed class MediaMuxer : Java.Lang.Object
[<Android.Runtime.Register("android/media/MediaMuxer", DoNotGenerateAcw=true)>]
type MediaMuxer = class
    inherit Object
Inheritance
MediaMuxer
Attributes

Remarks

MediaMuxer facilitates muxing elementary streams. Currently MediaMuxer supports MP4, Webm and 3GP file as the output. It also supports muxing B-frames in MP4 since Android Nougat.

It is generally used like this:

MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
            // More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat()
            // or MediaExtractor.getTrackFormat().
            MediaFormat audioFormat = new MediaFormat(...);
            MediaFormat videoFormat = new MediaFormat(...);
            int audioTrackIndex = muxer.addTrack(audioFormat);
            int videoTrackIndex = muxer.addTrack(videoFormat);
            ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize);
            boolean finished = false;
            BufferInfo bufferInfo = new BufferInfo();

            muxer.start();
            while(!finished) {
              // getInputBuffer() will fill the inputBuffer with one frame of encoded
              // sample from either MediaCodec or MediaExtractor, set isAudioSample to
              // true when the sample is audio data, set up all the fields of bufferInfo,
              // and return true if there are no more samples.
              finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);
              if (!finished) {
                int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex;
                muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo);
              }
            };
            muxer.stop();
            muxer.release();

<h4>Metadata Track</h4>

Per-frame metadata carries information that correlates with video or audio to facilitate offline processing. For example, gyro signals from the sensor can help video stabilization when doing offline processing. Metadata tracks are only supported when multiplexing to the MP4 container format. When adding a new metadata track, the MIME type format must start with prefix "application/" (for example, "application/gyro"). The format of the metadata is application-defined. Metadata timestamps must be in the same time base as video and audio timestamps. The generated MP4 file uses TextMetaDataSampleEntry (defined in section 12.3.3.2 of the ISOBMFF specification) to signal the metadata's MIME type.

MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
               // SetUp Video/Audio Tracks.
               MediaFormat audioFormat = new MediaFormat(...);
               MediaFormat videoFormat = new MediaFormat(...);
               int audioTrackIndex = muxer.addTrack(audioFormat);
               int videoTrackIndex = muxer.addTrack(videoFormat);

               // Setup Metadata Track
               MediaFormat metadataFormat = new MediaFormat(...);
               metadataFormat.setString(KEY_MIME, "application/gyro");
               int metadataTrackIndex = muxer.addTrack(metadataFormat);

               muxer.start();
               while(..) {
                   // Allocate bytebuffer and write gyro data(x,y,z) into it.
                   ByteBuffer metaData = ByteBuffer.allocate(bufferSize);
                   metaData.putFloat(x);
                   metaData.putFloat(y);
                   metaData.putFloat(z);
                   BufferInfo metaInfo = new BufferInfo();
                   // Associate this metadata with the video frame by setting
                   // the same timestamp as the video frame.
                   metaInfo.presentationTimeUs = currentVideoTrackTimeUs;
                   metaInfo.offset = 0;
                   metaInfo.flags = 0;
                   metaInfo.size = bufferSize;
                   muxer.writeSampleData(metadataTrackIndex, metaData, metaInfo);
               };
               muxer.stop();
               muxer.release();
             }

<h2 id=History>"History">Features and API History</h2>

The following table summarizes the feature support in different API version and containers. For API version numbers, see android.os.Build.VERSION_CODES.

<style> .api > tr > th, .api > tr > td { text-align: center; padding: 4px 4px; } .api > tr > th { vertical-align: bottom; } .api > tr > td { vertical-align: middle; } .sml > tr > th, .sml > tr > td { text-align: center; padding: 2px 4px; } .fn { text-align: center; }</style>

<table align="right" style="width: 0%"> <thead> <tbody class=api> <tr><th>Symbol</th> <th>Meaning</th></tr> </tbody> </thead> <tbody class=sml> <tr><td>&#9679;</td><td>Supported</td></tr> <tr><td>&#9675;</td><td>Not supported</td></tr> <tr><td>&#9639;</td><td>Supported in MP4/WebM/3GP</td></tr> <tr><td>&#8277;</td><td>Only Supported in MP4</td></tr> </tbody> </table> <table align="center" style="width: 100%;"> <thead class=api> <tr> <th rowspan=2>Feature</th> <th colspan="24">SDK Version</th> </tr> <tr> <th>18</th> <th>19</th> <th>20</th> <th>21</th> <th>22</th> <th>23</th> <th>24</th> <th>25</th> <th>26+</th> </tr> </thead> <tbody class=api> <tr> <td align="center">MP4 container</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> </tr> <td align="center">WebM container</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> <td>&#9679;</td> </tr> <td align="center">3GP container</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9679;</td> </tr> <td align="center">Muxing B-Frames(bi-directional predicted frames)</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#8277;</td> <td>&#8277;</td> <td>&#8277;</td> </tr> </tr> <td align="center">Muxing Single Video/Audio Track</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> <td>&#9639;</td> </tr> </tr> <td align="center">Muxing Multiple Video/Audio Tracks</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#8277;</td> </tr> </tr> <td align="center">Muxing Metadata Tracks</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#9675;</td> <td>&#8277;</td> </tr> </tbody> </table>

Java documentation for android.media.MediaMuxer.

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.

Constructors

MediaMuxer(FileDescriptor, MuxerOutputType)

Creates a media muxer that writes to the specified FileDescriptor.

MediaMuxer(String, MuxerOutputType)

Creates a media muxer that writes to the specified path.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
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

AddTrack(MediaFormat)

Adds a track with the specified format.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
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)
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()

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.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetLocation(Single, Single)

Set and store the geodata (latitude and longitude) in the output file.

SetOrientationHint(Int32)

Sets the orientation hint for output video playback.

Start()

Starts the muxer.

Stop()

Stops the muxer.

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)
WriteSampleData(Int32, ByteBuffer, MediaCodec+BufferInfo)

Writes an encoded sample into the muxer.

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)

Applies to