Android APIs
public static class

AudioFormat.Builder

extends Object
java.lang.Object
   ↳ android.media.AudioFormat.Builder

Class Overview

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.

AudioFormat is for instance used in 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
AudioFormat.Builder()
Constructs a new Builder with none of the format characteristics set.
AudioFormat.Builder(AudioFormat af)
Constructs a new Builder from a given AudioFormat.
Public Methods
AudioFormat build()
Combines all of the format characteristics that have been set and return a new AudioFormat object.
AudioFormat.Builder setChannelMask(int channelMask)
Sets the channel mask.
AudioFormat.Builder setEncoding(int encoding)
Sets the data encoding format.
AudioFormat.Builder setSampleRate(int sampleRate)
Sets the sample rate.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public AudioFormat.Builder ()

Added in API level 21

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

public AudioFormat.Builder (AudioFormat af)

Added in API level 21

Constructs a new Builder from a given AudioFormat.

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

Public Methods

public AudioFormat build ()

Added in API level 21

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

Returns

public AudioFormat.Builder setChannelMask (int channelMask)

Added in API level 21

Sets the channel mask.

Parameters
channelMask describes the configuration of the audio channels.

For output, the mask should be a combination of CHANNEL_OUT_FRONT_LEFT, CHANNEL_OUT_FRONT_CENTER, CHANNEL_OUT_FRONT_RIGHT, CHANNEL_OUT_SIDE_LEFT, CHANNEL_OUT_SIDE_RIGHT, CHANNEL_OUT_BACK_LEFT, CHANNEL_OUT_BACK_RIGHT.

for input, the mask should be CHANNEL_IN_MONO or CHANNEL_IN_STEREO. CHANNEL_IN_MONO is guaranteed to work on all devices.

Returns
  • the same Builder instance.

public AudioFormat.Builder setEncoding (int encoding)

Added in API level 21

Sets the data encoding format.

Returns
  • the same Builder instance.

public AudioFormat.Builder setSampleRate (int sampleRate)

Added in API level 21

Sets the sample rate.

Parameters
sampleRate the sample rate expressed in Hz
Returns
  • the same Builder instance.