to top
Android APIs
public final class

MediaMuxer

extends Object
java.lang.Object
   ↳ android.media.MediaMuxer

Class Overview

MediaMuxer facilitates muxing elementary streams. Currently only supports an mp4 file as the output and at most one audio and/or one video elementary stream.

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();
 

Summary

Nested Classes
class MediaMuxer.OutputFormat Defines the output format. 
Public Constructors
MediaMuxer(String path, int format)
Constructor.
Public Methods
int addTrack(MediaFormat format)
Adds a track with the specified format.
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 setLocation(float latitude, float longitude)
Set and store the geodata (latitude and longitude) in the output file.
void setOrientationHint(int degrees)
Sets the orientation hint for output video playback.
void start()
Starts the muxer.
void stop()
Stops the muxer.
void writeSampleData(int trackIndex, ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo)
Writes an encoded sample into the muxer.
Protected Methods
void finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public MediaMuxer (String path, int format)

Added in API level 18

Constructor. Creates a media muxer that writes to the specified path.

Parameters
path The path of the output media file.
format The format of the output media file.
Throws
IOException if failed to open the file for write

Public Methods

public int addTrack (MediaFormat format)

Added in API level 18

Adds a track with the specified format.

Parameters
format The media format for the track.
Returns

public void release ()

Added in API level 18

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.

public void setLocation (float latitude, float longitude)

Added in API level 19

Set and store the geodata (latitude and longitude) in the output file. This method should be called before start(). The geodata is stored in udta box if the output format is MUXER_OUTPUT_MPEG_4, and is ignored for other output formats. The geodata is stored according to ISO-6709 standard.

Parameters
latitude Latitude in degrees. Its value must be in the range [-90, 90].
longitude Longitude in degrees. Its value must be in the range [-180, 180].
Throws
IllegalArgumentException If the given latitude or longitude is out of range.
IllegalStateException If this method is called after start().

public void setOrientationHint (int degrees)

Added in API level 18

Sets the orientation hint for output video playback.

This method should be called before start(). Calling this method will not rotate the video frame when muxer is generating the file, but add a composition matrix containing the rotation angle in the output video if the output format is MUXER_OUTPUT_MPEG_4 so that a video player can choose the proper orientation for playback. Note that some video players may choose to ignore the composition matrix in a video during playback. By default, the rotation degree is 0.

Parameters
degrees the angle to be rotated clockwise in degrees. The supported angles are 0, 90, 180, and 270 degrees.

public void start ()

Added in API level 18

Starts the muxer.

Make sure this is called after addTrack(MediaFormat) and before writeSampleData(int, ByteBuffer, MediaCodec.BufferInfo).

public void stop ()

Added in API level 18

Stops the muxer.

Once the muxer stops, it can not be restarted.

public void writeSampleData (int trackIndex, ByteBuffer byteBuf, MediaCodec.BufferInfo bufferInfo)

Added in API level 18

Writes an encoded sample into the muxer.

The application needs to make sure that the samples are written into the right tracks. Also, it needs to make sure the samples for each track are written in chronological order (e.g. in the order they are provided by the encoder.)

Parameters
trackIndex The track index for this sample.
byteBuf The encoded sample.
bufferInfo The buffer information related to this sample. MediaMuxer uses the flags provided in MediaCodec.BufferInfo, to signal sync frames.

Protected Methods

protected void finalize ()

Added in API level 18

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.

Throws
Throwable