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

# FragmentedMp4Muxer

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/FragmentedMp4Muxer.java+class:androidx.media3.muxer.FragmentedMp4Muxer)

*** ** * ** ***


```
@UnstableApi
class FragmentedMp4Muxer : Muxer
```

<br />

*** ** * ** ***

A muxer for creating a fragmented MP4 file.

Muxer supports muxing of:

- Video Codecs:
  - AV1
  - MPEG-4
  - H.263
  - H.264 (AVC)
  - H.265 (HEVC)
  - VP9
  - APV
  - Dolby Vision (profiles 5, 8 and 9)
- Audio Codecs:
  - AAC
  - AMR-NB (Narrowband AMR)
  - AMR-WB (Wideband AMR)
  - Opus
  - Vorbis
  - Raw Audio
- Metadata

All the operations are performed on the caller thread.

To create a fragmented MP4 file, the caller must:

- Add tracks using `addTrack` which will return a track id.
- Use the associated track id when `writing samples` for that track.
- `close` the muxer when all data has been written.

Some key points:

- All tracks must be added before writing any samples.
- The caller is responsible for ensuring that samples of different track types are well interleaved by calling `writeSampleData` in an order that interleaves samples from different tracks.

## Summary

| ### Nested types |
|---|
| `class FragmentedMp4Muxer.Builder` A builder for `FragmentedMp4Muxer` instances. |

| ### Constants |
|---|---|
| `const Long` | `DEFAULT_FRAGMENT_DURATION_MS = 2000` The default fragment duration. |
| `const ImmutableList<String!>!` | `SUPPORTED_AUDIO_SAMPLE_MIME_TYPES` A list of supported audio `sample MIME types`. |
| `const ImmutableList<String!>!` | `SUPPORTED_VIDEO_SAMPLE_MIME_TYPES` A list of supported video `sample MIME types`. |

| ### Public functions |
|---|---|
| `Unit` | `addMetadataEntry(metadataEntry: Metadata.Entry!)` Adds metadata about the output file. |
| `Int` | `addTrack(format: Format!)` Adds a track of the given media format. |
| `Unit` | `close()` Closes the file. |
| `Unit` | `writeSampleData( trackId: Int, byteBuffer: ByteBuffer!, bufferInfo: BufferInfo! )` Writes encoded sample data. |

### Inherited functions

From [java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable.md.txt) `abstract Unit` `close()`

## Constants

### DEFAULT_FRAGMENT_DURATION_MS

```
const val DEFAULT_FRAGMENT_DURATION_MS = 2000: Long
```

The default fragment duration.

### SUPPORTED_AUDIO_SAMPLE_MIME_TYPES

```
const val SUPPORTED_AUDIO_SAMPLE_MIME_TYPES: ImmutableList<String!>!
```

A list of supported audio `sample MIME types`.

### SUPPORTED_VIDEO_SAMPLE_MIME_TYPES

```
const val SUPPORTED_VIDEO_SAMPLE_MIME_TYPES: ImmutableList<String!>!
```

A list of supported video `sample MIME types`.

## Public functions

### addMetadataEntry

```
fun addMetadataEntry(metadataEntry: Metadata.Entry!): Unit
```

Adds metadata about the output file.

List of supported `metadata entries`:

- `Mp4OrientationData`
- `Mp4LocationData`
- `Mp4TimestampData`
- `MdtaMetadataEntry`: Only `string type` or `float type` value is supported.
- `XmpData`

| Parameters |
|---|---|
| `metadataEntry: Metadata.Entry!` | The `metadata`. An `IllegalArgumentException` is thrown if the `metadata` is not supported. |

### addTrack

```
fun addTrack(format: Format!): Int
```

Adds a track of the given media format.

All tracks must be added before any samples are written to any track.

A metadata track (`TRACK_TYPE_METADATA`) or a track with an unknown type (`TRACK_TYPE_UNKNOWN`) is written as a text metadata track, unless the sample MIME type is `APPLICATION_ITUT_T35`, in which case it is written as a T35 metadata track.

| Parameters |
|---|---|
| `format: Format!` | The `Format` for the track. |

| Returns |
|---|---|
| `Int` | A unique track id. The track id is non-negative. It should be used in `writeSampleData`. |

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If the `format` is not supported or an error occurs while adding the track. |

### close

```
fun close(): Unit
```

Closes the file.

The muxer cannot be used anymore once this method is called, even if it throws. Implementations must make further calls to this method a no-op and, once it has been called, must throw an `IllegalStateException` from all other methods.

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If the muxer fails to finish writing the output. |

### writeSampleData

```
fun writeSampleData(
    trackId: Int,
    byteBuffer: ByteBuffer!,
    bufferInfo: BufferInfo!
): Unit
```

Writes encoded sample data.

Samples are written to the disk in batches. If ` sample copying` is disabled, the `byteBuffer` and the `bufferInfo` must not be modified after calling this method. Otherwise, they are copied and it is safe to modify them after this method returns.

Note: Out of order B-frames are currently not supported.

| Parameters |
|---|---|
| `trackId: Int` | The track id for which this sample is being written. |
| `byteBuffer: ByteBuffer!` | The encoded sample. The muxer takes ownership of the buffer if ` sample copying` is disabled. Otherwise, the position of the buffer is updated but the caller retains ownership. |
| `bufferInfo: BufferInfo!` | The `BufferInfo` related to this sample. |

| Throws |
|---|---|
| `androidx.media3.muxer.MuxerException` | If there is any error while writing data to the disk. |