Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/media/AudioFormat.Builder#pubctors) \| [Methods](https://developer.android.com/reference/android/media/AudioFormat.Builder#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/AudioFormat.Builder#inhmethods)

# AudioFormat.Builder

*** ** * ** ***

` public static class AudioFormat.Builder `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.AudioFormat.Builder |

<br />

*** ** * ** ***

Builder class for `https://developer.android.com/reference/android/media/AudioFormat` objects. Use this class to configure and create an AudioFormat instance. By setting format characteristics such as audio encoding, channel mask or sample rate, you indicate which of those are to vary from the default behavior on this device wherever this audio format is used. See `https://developer.android.com/reference/android/media/AudioFormat` for a complete description of the different parameters that can be used to configure an `AudioFormat` instance.

`https://developer.android.com/reference/android/media/AudioFormat` is for instance used in `https://developer.android.com/reference/android/media/AudioTrack#AudioTrack(android.media.AudioAttributes,%20android.media.AudioFormat,%20int,%20int,%20int)`. In this constructor, every format characteristic set on the `Builder` (e.g. with `https://developer.android.com/reference/android/media/AudioFormat.Builder#setSampleRate(int)`) will alter the default values used by an `AudioTrack`. In this case for audio playback with `AudioTrack`, the sample rate set in the `Builder` would override the platform output sample rate which would otherwise be selected by default.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder#Builder()() ` Constructs a new Builder with none of the format characteristics set. |
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder#Builder(android.media.AudioFormat)(https://developer.android.com/reference/android/media/AudioFormat af) ` Constructs a new Builder from a given `https://developer.android.com/reference/android/media/AudioFormat`. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/media/AudioFormat` | ` https://developer.android.com/reference/android/media/AudioFormat.Builder#build()() ` Combines all of the format characteristics that have been set and return a new `https://developer.android.com/reference/android/media/AudioFormat` object. |
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder` | ` https://developer.android.com/reference/android/media/AudioFormat.Builder#setChannelAcnMask(int)(int channelAcnMask) ` Sets the Ambisonics channel mask. |
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder` | ` https://developer.android.com/reference/android/media/AudioFormat.Builder#setChannelIndexMask(int)(int channelIndexMask) ` Sets the channel index mask. |
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder` | ` https://developer.android.com/reference/android/media/AudioFormat.Builder#setChannelMask(int)(int channelMask) ` Sets the channel position mask. |
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder` | ` https://developer.android.com/reference/android/media/AudioFormat.Builder#setEncoding(int)(int encoding) ` Sets the data encoding format. |
| ` https://developer.android.com/reference/android/media/AudioFormat.Builder` | ` https://developer.android.com/reference/android/media/AudioFormat.Builder#setSampleRate(int)(int sampleRate) ` Sets the sample rate. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### Builder

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Builder ()
```

Constructs a new Builder with none of the format characteristics set.

<br />

### Builder

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Builder (AudioFormat af)
```

Constructs a new Builder from a given `https://developer.android.com/reference/android/media/AudioFormat`.

<br />

| Parameters ||
|---|---|
| `af` | `AudioFormat`: the `https://developer.android.com/reference/android/media/AudioFormat` object whose data will be reused in the new Builder. <br /> |

## Public methods

### build

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AudioFormat build ()
```

Combines all of the format characteristics that have been set and return a new `https://developer.android.com/reference/android/media/AudioFormat` object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/AudioFormat` | a new `https://developer.android.com/reference/android/media/AudioFormat` object <br /> |

### setChannelAcnMask

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AudioFormat.Builder setChannelAcnMask (int channelAcnMask)
```

Sets the Ambisonics channel mask. The Ambisonics channel mask specifies that the audio data is in Ambisonics format, using the ACN (Ambisonics Channel Number) ordering and SN3D normalization. The mask indicates the order of the Ambisonics content and whether it is a full-sphere or horizontal-only representation.

<br />

| Parameters ||
|---|---|
| `channelAcnMask` | `int`: the Ambisonics channel mask, e.g. `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_ACN_ORDER_1`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_ACN_ORDER_1_HRZ`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/AudioFormat.Builder` | the same `Builder` instance. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `channelAcnMask` is 0 or if a channel index mask is already set and its channel count does not match the channel count derived from `channelAcnMask`. |

### setChannelIndexMask

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AudioFormat.Builder setChannelIndexMask (int channelIndexMask)
```

Sets the channel index mask. A channel index mask specifies the association of audio samples in the frame with numbered endpoint channels. The i-th bit in the channel index mask corresponds to the i-th endpoint channel. For example, an endpoint with four channels is represented as index mask bits 0 through 3. This [](https://developer.android.com/reference/android/media/AudioFormat.Builder#channelIndexMask>description of channel
index masks</a> covers the concept in more details.
See <code translate=)[setChannelMask(int)](https://developer.android.com/reference/android/media/AudioFormat.Builder#setChannelMask(int)) for a positional mask interpretation.

Both `https://developer.android.com/reference/android/media/AudioTrack` and `https://developer.android.com/reference/android/media/AudioRecord` support a channel index mask. If a channel index mask is specified it is used, otherwise the channel position mask specified by `setChannelMask` is used. For `AudioTrack` and `AudioRecord`, a channel position mask is not required if a channel index mask is specified.

<br />

| Parameters ||
|---|---|
| `channelIndexMask` | `int`: describes the configuration of the audio channels. For output, the `channelIndexMask` is an OR-ed combination of bits representing the mapping of `AudioTrack` write samples to output sink channels. For example, a mask of `0xa`, or binary `1010`, means the `AudioTrack` write frame consists of two samples, which are routed to the second and the fourth channels of the output sink. Unmatched output sink channels are zero filled and unmatched `AudioTrack` write samples are dropped. For input, the `channelIndexMask` is an OR-ed combination of bits representing the mapping of input source channels to `AudioRecord` read samples. For example, a mask of `0x5`, or binary `101`, will read from the first and third channel of the input source device and store them in the first and second sample of the `AudioRecord` read frame. Unmatched input source channels are dropped and unmatched `AudioRecord` read samples are zero filled. |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/AudioFormat.Builder` | the same `Builder` instance. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the channel index mask is invalid or if both channel index mask and channel position mask are specified but do not have the same channel count. |

### setChannelMask

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AudioFormat.Builder setChannelMask (int channelMask)
```

Sets the channel position mask. The channel position mask specifies the association between audio samples in a frame with named endpoint channels. The samples in the frame correspond to the named set bits in the channel position mask, in ascending bit order. See `https://developer.android.com/reference/android/media/AudioFormat.Builder#setChannelIndexMask(int)` to specify channels based on endpoint numbered channels. This [description of channel position masks](https://developer.android.com/reference/android/media/AudioFormat.Builder#channelPositionMask) covers the concept in more details.

<br />

| Parameters ||
|---|---|
| `channelMask` | `int`: describes the configuration of the audio channels. For output, the channelMask can be an OR-ed combination of channel position masks, e.g. `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_FRONT_LEFT`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_FRONT_RIGHT`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_FRONT_CENTER`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_LOW_FREQUENCY` `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_BACK_LEFT`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_BACK_RIGHT`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_BACK_CENTER`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_SIDE_LEFT`, `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_OUT_SIDE_RIGHT`. For output or `https://developer.android.com/reference/android/media/AudioTrack`, channel position masks which do not contain matched left/right pairs are invalid. For input or `https://developer.android.com/reference/android/media/AudioRecord`, the mask should be `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_IN_MONO` or `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_IN_STEREO`. `https://developer.android.com/reference/android/media/AudioFormat#CHANNEL_IN_MONO` is guaranteed to work on all devices. |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/AudioFormat.Builder` | the same `Builder` instance. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the channel mask is invalid or if both channel index mask and channel position mask are specified but do not have the same channel count. |

### setEncoding

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AudioFormat.Builder setEncoding (int encoding)
```

Sets the data encoding format.

<br />

| Parameters ||
|---|---|
| `encoding` | `int`: the specified encoding or default. Value is one of the following: - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DEFAULT` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_PCM_16BIT` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_PCM_8BIT` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_PCM_FLOAT` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AC3` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_E_AC3` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DTS` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DTS_HD` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_MP3` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AAC_LC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AAC_HE_V1` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AAC_HE_V2` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IEC61937` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DOLBY_TRUEHD` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AAC_ELD` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AAC_XHE` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AC4` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_AC4_L4` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_E_AC3_JOC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DOLBY_MAT` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_OPUS` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_PCM_24BIT_PACKED` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_PCM_32BIT` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_MPEGH_BL_L3` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_MPEGH_BL_L4` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_MPEGH_LC_L3` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_MPEGH_LC_L4` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DTS_UHD_P1` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DRA` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DTS_HD_MA` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DTS_UHD_P2` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_DSD` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_ENHANCED_PROFILE_AAC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_ENHANCED_PROFILE_FLAC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_ENHANCED_PROFILE_OPUS` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_ENHANCED_PROFILE_PCM` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_PROFILE_AAC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_PROFILE_FLAC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_PROFILE_OPUS` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_BASE_PROFILE_PCM` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_SIMPLE_PROFILE_AAC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_SIMPLE_PROFILE_FLAC` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_SIMPLE_PROFILE_OPUS` - `https://developer.android.com/reference/android/media/AudioFormat#ENCODING_IAMF_SIMPLE_PROFILE_PCM` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/AudioFormat.Builder` | the same Builder instance. <br /> |

| Throws ||
|---|---|
|   | java.lang.IllegalArgumentException |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` |   |

### setSampleRate

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public AudioFormat.Builder setSampleRate (int sampleRate)
```

Sets the sample rate.

<br />

| Parameters ||
|---|---|
| `sampleRate` | `int`: the sample rate expressed in Hz <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/AudioFormat.Builder` | the same Builder instance. <br /> |

| Throws ||
|---|---|
|   | java.lang.IllegalArgumentException |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` |   |