java.lang.Object | |
↳ | android.media.MediaMuxer |
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();
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
MediaMuxer.OutputFormat | Defines the output format. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a track with the specified format.
| |||||||||||
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.
| |||||||||||
Set and store the geodata (latitude and longitude) in the output file.
| |||||||||||
Sets the orientation hint for output video playback.
| |||||||||||
Starts the muxer.
| |||||||||||
Stops the muxer.
| |||||||||||
Writes an encoded sample into the muxer.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Constructor. Creates a media muxer that writes to the specified path.
path | The path of the output media file. |
---|---|
format | The format of the output media file. |
IOException | if failed to open the file for write |
---|
Adds a track with the specified format.
format | The media format for the track. |
---|
writeSampleData(int, ByteBuffer, MediaCodec.BufferInfo)
.
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.
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.
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]. |
IllegalArgumentException | If the given latitude or longitude is out of range. |
---|---|
IllegalStateException | If this method is called after start() .
|
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.
degrees | the angle to be rotated clockwise in degrees. The supported angles are 0, 90, 180, and 270 degrees. |
---|
Starts the muxer.
Make sure this is called after addTrack(MediaFormat)
and before
writeSampleData(int, ByteBuffer, MediaCodec.BufferInfo)
.
Stops the muxer.
Once the muxer stops, it can not be restarted.
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.)
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.
|
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.
Throwable |
---|