# MediaMuxerCompat

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

*** ** * ** ***


```
public final class MediaMuxerCompat
```

<br />

*** ** * ** ***

A drop-in replacement for `https://developer.android.com/reference/android/media/MediaMuxer.html` that provides similar functionality, based on the `media3.muxer` logic.

Supported file formats are `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#OUTPUT_FORMAT_MP4()` and `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#OUTPUT_FORMAT_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 |
|---|
| `@https://developer.android.com/reference/java/lang/annotation/Documented.html @https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#SOURCE) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE_USE) @https://developer.android.com/reference/androidx/annotation/IntDef(value = ) @https://developer.android.com/reference/androidx/media3/common/util/UnstableApi public annotation https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat` The output file format. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#OUTPUT_FORMAT_MP4() = 0` The MP4 file format. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#OUTPUT_FORMAT_WEBM() = 1` The WebM file format. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#MediaMuxerCompat(java.io.FileDescriptor,int)( https://developer.android.com/reference/java/io/FileDescriptor.html fileDescriptor, @https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat int outputFormat )` Creates an instance. |
| `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#MediaMuxerCompat(java.lang.String,int)( https://developer.android.com/reference/java/lang/String.html filePath, @https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat int outputFormat )` Creates an instance. |

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#addTrack(android.media.MediaFormat)(https://developer.android.com/reference/android/media/MediaFormat.html format)` Adds a track of the given media format. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#release()()` Releases the underlying resources. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#setLocation(float,float)( @https://developer.android.com/reference/androidx/annotation/FloatRange(from = "-90.0", to = 90.0) float latitude, @https://developer.android.com/reference/androidx/annotation/FloatRange(from = "-180.0", to = 180.0) float longitude )` Sets the location of the media file. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#setOrientationHint(int)(int degrees)` Sets the orientation hint for the media file. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#start()()` Starts the muxer. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#stop()()` Stops the muxer. |
| `void` | `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#writeSampleData(int,java.nio.ByteBuffer,android.media.MediaCodec.BufferInfo)( int trackIndex, https://developer.android.com/reference/java/nio/ByteBuffer.html byteBuffer, https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html bufferInfo )` Writes encoded sample data. |

## Constants

### OUTPUT_FORMAT_MP4

```
public static final int OUTPUT_FORMAT_MP4 = 0
```

The MP4 file format.

### OUTPUT_FORMAT_WEBM

```
public static final int OUTPUT_FORMAT_WEBM = 1
```

The WebM file format.

## Public constructors

### MediaMuxerCompat

```
public MediaMuxerCompat(
    FileDescriptor fileDescriptor,
    @MediaMuxerCompat.OutputFormat int outputFormat
)
```

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 |
|---|---|
| `https://developer.android.com/reference/java/io/FileDescriptor.html fileDescriptor` | A `https://developer.android.com/reference/java/io/FileDescriptor.html` for the output media file. It must represent a local file that is open in read-write mode. |
| `@https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat int outputFormat` | The `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs while performing an I/O operation. |

### MediaMuxerCompat

```
public MediaMuxerCompat(
    String filePath,
    @MediaMuxerCompat.OutputFormat int outputFormat
)
```

Creates an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html filePath` | The path of the output media file. |
| `@https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat int outputFormat` | The `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat.OutputFormat`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs while performing an I/O operation. |

## Public methods

### addTrack

```
public int addTrack(MediaFormat format)
```

Adds a track of the given media format.

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

`https://developer.android.com/reference/android/media/MediaFormat.html#KEY_CAPTURE_RATE` is used to write `https://developer.android.com/reference/androidx/media3/container/MdtaMetadataEntry#KEY_ANDROID_CAPTURE_FPS()` metadata in the MP4 file.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/media/MediaFormat.html format` | The `https://developer.android.com/reference/android/media/MediaFormat.html` of the track. |

| Returns |
|---|---|
| `int` | A track index for this track, which should be passed to `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#writeSampleData(int,java.nio.ByteBuffer,android.media.MediaCodec.BufferInfo)`. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#addTrack(android.media.MediaFormat)` |   |

### release

```
public void release()
```

Releases the underlying resources.

It should be called after `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#stop()`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#release()` |   |

### setLocation

```
public void setLocation(
    @FloatRange(from = "-90.0", to = 90.0) float latitude,
    @FloatRange(from = "-180.0", to = 180.0) float longitude
)
```

Sets the location of the media file.

It must be called before `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#start()`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/FloatRange(from = "-90.0", to = 90.0) float latitude` | The latitude, in degrees. Its value must be in the range \[-90, 90\]. |
| `@https://developer.android.com/reference/androidx/annotation/FloatRange(from = "-180.0", to = 180.0) float longitude` | The longitude, in degrees. Its value must be in the range \[-180, 180\]. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#setLocation(float, float)` |   |

### setOrientationHint

```
public void setOrientationHint(int degrees)
```

Sets the orientation hint for the media file.

It must be called before `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#start()`.

| Parameters |
|---|---|
| `int degrees` | The orientation, in degrees. The supported values are 0, 90, 180 and 270 (degrees). |

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#setOrientationHint(int)` |   |

### start

```
public void start()
```

Starts the muxer.

This must be called after `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#addTrack(android.media.MediaFormat)` and before `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#writeSampleData(int,java.nio.ByteBuffer,android.media.MediaCodec.BufferInfo)`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#start()` |   |

### stop

```
public void stop()
```

Stops the muxer.

Once the muxer is stopped, it can not be restarted.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#stop()` |   |

### writeSampleData

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

Writes encoded sample data.

To set the duration of the last sample in a track, an additional empty buffer (`
bufferInfo.size = 0`) with `https://developer.android.com/reference/android/media/MediaCodec.html#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 |
|---|---|
| `int trackIndex` | The track index, previously returned by `https://developer.android.com/reference/androidx/media3/muxer/MediaMuxerCompat#addTrack(android.media.MediaFormat)`. |
| `https://developer.android.com/reference/java/nio/ByteBuffer.html byteBuffer` | A buffer containing the sample data to write. |
| `https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html bufferInfo` | The `https://developer.android.com/reference/android/media/MediaCodec.BufferInfo.html` of the sample. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/MediaMuxer.html#writeSampleData(int, java.nio.ByteBuffer, android.media.MediaCodec.BufferInfo)` |   |