> Documentation index: https://developer.android.com/reference/llms.txt

# MediaMuxerCompat

Artifact: [androidx.media3:media3-muxer](https://developer.android.com/jetpack/androidx/releases/media3.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/media3/muxer/MediaMuxerCompat.java+class:androidx.media3.muxer.MediaMuxerCompat)

*** ** * ** ***


```
class MediaMuxerCompat
```

<br />

*** ** * ** ***

A drop-in replacement for `MediaMuxer` that provides similar functionality, based on the `media3.muxer` logic.

Supported file formats are `MP4` and `WEBM`.

Supported codecs are:

- For MP4 file format:
  - 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
- For WebM file format:
  - Video Codecs:
    - VP8
    - VP9
  - Audio Codecs:
    - Opus
    - Vorbis

All the methods should be called from the same thread.

All the operations are performed on the caller thread.

## Summary

| ### Nested types |
|---|
| `@Documented @Retention(value = RetentionPolicy.SOURCE) @Target(value = ElementType.TYPE_USE) @IntDef(value = ) @UnstableApi annotation MediaMuxerCompat.OutputFormat` The output file format. |

| ### Constants |
|---|---|
| `const Int` | `OUTPUT_FORMAT_MP4 = 0` The MP4 file format. |
| `const Int` | `OUTPUT_FORMAT_WEBM = 1` The WebM file format. |

| ### Public constructors |
|---|
| `MediaMuxerCompat( fileDescriptor: FileDescriptor!, @MediaMuxerCompat.OutputFormat outputFormat: Int )` Creates an instance. |
| `MediaMuxerCompat( filePath: String!, @MediaMuxerCompat.OutputFormat outputFormat: Int )` 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( latitude: @FloatRange(from = "-90.0", to = 90.0) Float, longitude: @FloatRange(from = "-180.0", to = 180.0) Float )` 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( trackIndex: Int, byteBuffer: ByteBuffer!, bufferInfo: MediaCodec.BufferInfo! )` Writes encoded sample data. |

## Constants

### OUTPUT_FORMAT_MP4

```
const val OUTPUT_FORMAT_MP4 = 0: Int
```

The MP4 file format.

### OUTPUT_FORMAT_WEBM

```
const val OUTPUT_FORMAT_WEBM = 1: Int
```

The WebM file format.

## 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 `FileDescriptor` for the output media file. It must represent a local file that is open in read-write mode. |
| `@MediaMuxerCompat.OutputFormat outputFormat: Int` | The `OutputFormat`. |

| 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 `OutputFormat`. |

| 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 `MediaFormat` of the track. |

| Returns |
|---|---|
| `Int` | A track index for this track, which should be passed to `writeSampleData`. |

| Throws |
|---|---|
| `java.lang.IllegalArgumentException` | If the `format` is not supported. |

| 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 `addTrack`. |
| `byteBuffer: ByteBuffer!` | A buffer containing the sample data to write. |
| `bufferInfo: MediaCodec.BufferInfo!` | The `MediaCodec.BufferInfo` of the sample. |

| See also |
|---|---|
| `writeSampleData` |   |