Added in API level 21

Builder

class Builder
kotlin.Any
   ↳ android.media.AudioFormat.Builder

Builder class for 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 AudioFormat for a complete description of the different parameters that can be used to configure an AudioFormat instance.

AudioFormat is for instance used in AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int). In this constructor, every format characteristic set on the Builder (e.g. with 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

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

Constructs a new Builder from a given AudioFormat.

Public methods
open AudioFormat!

Combines all of the format characteristics that have been set and return a new AudioFormat object.

open AudioFormat.Builder
setChannelIndexMask(channelIndexMask: Int)

Sets the channel index mask.

open AudioFormat.Builder
setChannelMask(channelMask: Int)

Sets the channel position mask.

open AudioFormat.Builder!
setEncoding(encoding: Int)

Sets the data encoding format.

open AudioFormat.Builder!
setSampleRate(sampleRate: Int)

Sets the sample rate.

Public constructors

Builder

Added in API level 21
Builder()

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

Builder

Added in API level 21
Builder(af: AudioFormat!)

Constructs a new Builder from a given AudioFormat.

Parameters
af AudioFormat!: the AudioFormat object whose data will be reused in the new Builder.

Public methods

build

Added in API level 21
open fun build(): AudioFormat!

Combines all of the format characteristics that have been set and return a new AudioFormat object.

Return
AudioFormat! a new AudioFormat object

setChannelIndexMask

Added in API level 23
open fun setChannelIndexMask(channelIndexMask: Int): AudioFormat.Builder

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 setChannelMask(int) for a positional mask interpretation.

Both AudioTrack and 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.

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.

Return
AudioFormat.Builder the same Builder instance. This value cannot be null.
Exceptions
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
open fun setChannelMask(channelMask: Int): AudioFormat.Builder

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 setChannelIndexMask(int) to specify channels based on endpoint numbered channels. This description of channel position masks covers the concept in more details.

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. AudioFormat#CHANNEL_OUT_FRONT_LEFT, AudioFormat#CHANNEL_OUT_FRONT_RIGHT, AudioFormat#CHANNEL_OUT_FRONT_CENTER, AudioFormat#CHANNEL_OUT_LOW_FREQUENCY AudioFormat#CHANNEL_OUT_BACK_LEFT, AudioFormat#CHANNEL_OUT_BACK_RIGHT, AudioFormat#CHANNEL_OUT_BACK_CENTER, AudioFormat#CHANNEL_OUT_SIDE_LEFT, AudioFormat#CHANNEL_OUT_SIDE_RIGHT.

For output or AudioTrack, channel position masks which do not contain matched left/right pairs are invalid.

For input or AudioRecord, the mask should be AudioFormat#CHANNEL_IN_MONO or AudioFormat#CHANNEL_IN_STEREO. AudioFormat#CHANNEL_IN_MONO is guaranteed to work on all devices.

Return
AudioFormat.Builder the same Builder instance. This value cannot be null.
Exceptions
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
open fun setEncoding(encoding: Int): AudioFormat.Builder!

Sets the data encoding format.

Parameters
encoding Int: the specified encoding or default. Value is android.media.AudioFormat#ENCODING_DEFAULT, android.media.AudioFormat#ENCODING_PCM_16BIT, android.media.AudioFormat#ENCODING_PCM_8BIT, android.media.AudioFormat#ENCODING_PCM_FLOAT, android.media.AudioFormat#ENCODING_AC3, android.media.AudioFormat#ENCODING_E_AC3, android.media.AudioFormat#ENCODING_DTS, android.media.AudioFormat#ENCODING_DTS_HD, android.media.AudioFormat#ENCODING_MP3, android.media.AudioFormat#ENCODING_AAC_LC, android.media.AudioFormat#ENCODING_AAC_HE_V1, android.media.AudioFormat#ENCODING_AAC_HE_V2, android.media.AudioFormat#ENCODING_IEC61937, android.media.AudioFormat#ENCODING_DOLBY_TRUEHD, android.media.AudioFormat#ENCODING_AAC_ELD, android.media.AudioFormat#ENCODING_AAC_XHE, android.media.AudioFormat#ENCODING_AC4, android.media.AudioFormat#ENCODING_E_AC3_JOC, android.media.AudioFormat#ENCODING_DOLBY_MAT, android.media.AudioFormat#ENCODING_OPUS, android.media.AudioFormat#ENCODING_PCM_24BIT_PACKED, android.media.AudioFormat#ENCODING_PCM_32BIT, android.media.AudioFormat#ENCODING_MPEGH_BL_L3, android.media.AudioFormat#ENCODING_MPEGH_BL_L4, android.media.AudioFormat#ENCODING_MPEGH_LC_L3, android.media.AudioFormat#ENCODING_MPEGH_LC_L4, android.media.AudioFormat#ENCODING_DTS_UHD_P1, android.media.AudioFormat#ENCODING_DRA, android.media.AudioFormat#ENCODING_DTS_HD_MA, android.media.AudioFormat#ENCODING_DTS_UHD_P2, or android.media.AudioFormat#ENCODING_DSD
Return
AudioFormat.Builder! the same Builder instance.
Exceptions
java.lang.IllegalArgumentException

setSampleRate

Added in API level 21
open fun setSampleRate(sampleRate: Int): AudioFormat.Builder!

Sets the sample rate.

Parameters
sampleRate Int: the sample rate expressed in Hz
Return
AudioFormat.Builder! the same Builder instance.
Exceptions
java.lang.IllegalArgumentException