Added in API level 23

Builder


open class Builder
kotlin.Any
   ↳ android.media.AudioRecord.Builder

Builder class for AudioRecord objects. Use this class to configure and create an AudioRecord instance. By setting the recording source and audio format parameters, you indicate which of those vary from the default behavior on the device.

Here is an example where Builder is used to specify all AudioFormat parameters, to be used by a new AudioRecord instance:

AudioRecord recorder = new AudioRecord.Builder()
          .setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION)
          .setAudioFormat(new AudioFormat.Builder()
                  .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                  .setSampleRate(32000)
                  .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
                  .build())
          .setBufferSizeInBytes(2*minBuffSize)
          .build();
  

If the audio source is not set with setAudioSource(int), MediaRecorder.AudioSource#DEFAULT is used.
If the audio format is not specified or is incomplete, its channel configuration will be AudioFormat#CHANNEL_IN_MONO, and the encoding will be AudioFormat#ENCODING_PCM_16BIT. The sample rate will depend on the device actually selected for capture and can be queried with getSampleRate() method.
If the buffer size is not specified with setBufferSizeInBytes(int), the minimum buffer size for the source is used.

Summary

Public constructors

Constructs a new Builder with the default values as described above.

Public methods
open AudioRecord!


Requires android.Manifest.permission#RECORD_AUDIO

open AudioRecord.Builder!

Sets the format of the audio data to be captured.

open AudioRecord.Builder

Sets the AudioRecord to record audio played by other apps.

open AudioRecord.Builder!

open AudioRecord.Builder!
setBufferSizeInBytes(bufferSizeInBytes: Int)

Sets the total size (in bytes) of the buffer where audio data is written during the recording.

open AudioRecord.Builder
setContext(context: Context)

Sets the context the record belongs to.

open AudioRecord.Builder
setPrivacySensitive(privacySensitive: Boolean)

Indicates that this capture request is privacy sensitive and that any concurrent capture is not permitted.

Public constructors

Builder

Added in API level 23
Builder()

Constructs a new Builder with the default values as described above.

Public methods

build

Added in API level 23
open fun build(): AudioRecord!


Requires android.Manifest.permission#RECORD_AUDIO

Return
AudioRecord! a new AudioRecord instance successfully initialized with all the parameters set on this Builder.
Exceptions
java.lang.UnsupportedOperationException if the parameters set on the Builder were incompatible, if the parameters are not supported by the device, if the caller does not hold the appropriate permissions, or if the device was not available.

setAudioFormat

Added in API level 23
open fun setAudioFormat(format: AudioFormat): AudioRecord.Builder!

Sets the format of the audio data to be captured.

Parameters
format AudioFormat: a non-null AudioFormat instance
Return
AudioRecord.Builder! the same Builder instance.
Exceptions
java.lang.IllegalArgumentException

setAudioPlaybackCaptureConfig

Added in API level 29
open fun setAudioPlaybackCaptureConfig(config: AudioPlaybackCaptureConfiguration): AudioRecord.Builder

Sets the AudioRecord to record audio played by other apps.

Parameters
config AudioPlaybackCaptureConfiguration: Defines what apps to record audio from (i.e., via either their uid or the type of audio). This value cannot be null.
Return
AudioRecord.Builder This value cannot be null.
Exceptions
java.lang.IllegalStateException if called in conjunction with setAudioSource(int).
java.lang.NullPointerException if config is null.

setBufferSizeInBytes

Added in API level 23
open fun setBufferSizeInBytes(bufferSizeInBytes: Int): AudioRecord.Builder!

Sets the total size (in bytes) of the buffer where audio data is written during the recording. New audio data can be read from this buffer in smaller chunks than this size. See getMinBufferSize(int,int,int) to determine the minimum required buffer size for the successful creation of an AudioRecord instance. Since bufferSizeInBytes may be internally increased to accommodate the source requirements, use getBufferSizeInFrames() to determine the actual buffer size in frames.

Parameters
bufferSizeInBytes Int: a value strictly greater than 0
Return
AudioRecord.Builder! the same Builder instance.
Exceptions
java.lang.IllegalArgumentException

setContext

Added in API level 31
open fun setContext(context: Context): AudioRecord.Builder

Sets the context the record belongs to. This context will be used to pull information, such as android.content.AttributionSource and device specific session ids, which will be associated with the AudioRecord the AudioRecord. However, the context itself will not be retained by the AudioRecord.

Parameters
context Context: a non-null Context instance
Return
AudioRecord.Builder the same Builder instance. This value cannot be null.

setPrivacySensitive

Added in API level 30
open fun setPrivacySensitive(privacySensitive: Boolean): AudioRecord.Builder

Indicates that this capture request is privacy sensitive and that any concurrent capture is not permitted.

The default is not privacy sensitive except when the audio source set with setAudioSource(int) is MediaRecorder.AudioSource#VOICE_COMMUNICATION or MediaRecorder.AudioSource#CAMCORDER.

Always takes precedence over default from audio source when set explicitly.

Using this API is only permitted when the audio source is one of:

Invoking build() will throw an UnsupportedOperationException if this condition is not met.
Parameters
privacySensitive Boolean: True if capture from this AudioRecord must be marked as privacy sensitive, false otherwise.
Return
AudioRecord.Builder This value cannot be null.