MediaMuxerCompat
class MediaMuxerCompat
A drop-in replacement for MediaMuxer that provides similar functionality, based on the media3.muxer logic.
Currently only MP4 file format is supported.
Supported codecs are:
- Video Codecs:
- AV1
- MPEG-4
- H.263
- H.264 (AVC)
- H.265 (HEVC)
- VP9
- APV
- Dolby Vision
- Audio Codecs:
- AAC
- AMR-NB (Narrowband AMR)
- AMR-WB (Wideband AMR)
- Opus
- Vorbis
- Raw Audio
All the methods should be called from the same thread.
All the operations are performed on the caller thread.
Summary
Nested types |
|---|
@DocumentedThe output file format. |
Constants |
|
|---|---|
const Int |
The MP4 file format. |
Public constructors |
|---|
MediaMuxerCompat(Creates an instance. |
MediaMuxerCompat(Creates an instance. |
Public functions |
|
|---|---|
Int |
addTrack(format: MediaFormat!)Adds a track of the given media format. |
Unit |
release()Releases the underlying resources. |
Unit |
setLocation(Sets the location of the media file. |
Unit |
setOrientationHint(degrees: Int)Sets the orientation hint for the media file. |
Unit |
start()Starts the muxer. |
Unit |
stop()Stops the muxer. |
Unit |
writeSampleData(Writes encoded sample data. |
Constants
Public constructors
MediaMuxerCompat
MediaMuxerCompat(
fileDescriptor: FileDescriptor!,
@MediaMuxerCompat.OutputFormat outputFormat: Int
)
Creates an instance.
It is the caller's responsibility to close the fileDescriptor, which is safe to do so as soon as this call returns.
| Parameters | |
|---|---|
fileDescriptor: FileDescriptor! |
A |
@MediaMuxerCompat.OutputFormat outputFormat: Int |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs while performing an I/O operation. |
MediaMuxerCompat
MediaMuxerCompat(
filePath: String!,
@MediaMuxerCompat.OutputFormat outputFormat: Int
)
Creates an instance.
| Parameters | |
|---|---|
filePath: String! |
The path of the output media file. |
@MediaMuxerCompat.OutputFormat outputFormat: Int |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs while performing an I/O operation. |
Public functions
addTrack
fun addTrack(format: MediaFormat!): Int
Adds a track of the given media format.
All tracks must be added before any samples are written to any track.
KEY_CAPTURE_RATE is used to write KEY_ANDROID_CAPTURE_FPS metadata in the MP4 file.
| Parameters | |
|---|---|
format: MediaFormat! |
The |
| Returns | |
|---|---|
Int |
A track index for this track, which should be passed to |
| See also | |
|---|---|
addTrack |
release
fun release(): Unit
Releases the underlying resources.
It should be called after stop.
| See also | |
|---|---|
release |
setLocation
fun setLocation(
latitude: @FloatRange(from = "-90.0", to = 90.0) Float,
longitude: @FloatRange(from = "-180.0", to = 180.0) Float
): Unit
Sets the location of the media file.
It must be called before start.
| Parameters | |
|---|---|
latitude: @FloatRange(from = "-90.0", to = 90.0) Float |
The latitude, in degrees. Its value must be in the range [-90, 90]. |
longitude: @FloatRange(from = "-180.0", to = 180.0) Float |
The longitude, in degrees. Its value must be in the range [-180, 180]. |
| See also | |
|---|---|
setLocation |
setOrientationHint
fun setOrientationHint(degrees: Int): Unit
Sets the orientation hint for the media file.
It must be called before start.
| Parameters | |
|---|---|
degrees: Int |
The orientation, in degrees. The supported values are 0, 90, 180 and 270 (degrees). |
| See also | |
|---|---|
setOrientationHint |
start
fun start(): Unit
Starts the muxer.
This must be called after addTrack and before writeSampleData.
| See also | |
|---|---|
start |
stop
fun stop(): Unit
Stops the muxer.
Once the muxer is stopped, it can not be restarted.
| See also | |
|---|---|
stop |
writeSampleData
fun writeSampleData(
trackIndex: Int,
byteBuffer: ByteBuffer!,
bufferInfo: MediaCodec.BufferInfo!
): Unit
Writes encoded sample data.
To set the duration of the last sample in a track, an additional empty buffer (
bufferInfo.size = 0) with BUFFER_FLAG_END_OF_STREAM flag and a suitable presentation timestamp must be passed as the last sample of that track. This timestamp should be the sum of the desired duration and the presentation timestamp of the original last sample. If no explicit END_OF_STREAM sample is provided, the last sample's duration will be equal to the previous sample's duration.
| Parameters | |
|---|---|
trackIndex: Int |
The track index, previously returned by |
byteBuffer: ByteBuffer! |
A buffer containing the sample data to write. |
bufferInfo: MediaCodec.BufferInfo! |
The |
| See also | |
|---|---|
writeSampleData |