Skip to content

Most visited

Recently visited

navigation

AudioRecord.Builder

public static class AudioRecord.Builder
extends Object

java.lang.Object
   ↳ 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())
         .setBufferSize(2*minBuffSize)
         .build();
 

If the audio source is not set with setAudioSource(int), DEFAULT is used.
If the audio format is not specified or is incomplete, its channel configuration will be CHANNEL_IN_MONO, and the encoding will be 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

AudioRecord.Builder()

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

Public methods

AudioRecord build()
AudioRecord.Builder setAudioFormat(AudioFormat format)

Sets the format of the audio data to be captured.

AudioRecord.Builder setAudioSource(int source)
AudioRecord.Builder setBufferSizeInBytes(int bufferSizeInBytes)

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

Inherited methods

From class java.lang.Object

Public constructors

AudioRecord.Builder

added in API level 23
AudioRecord.Builder ()

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

Public methods

build

added in API level 23
AudioRecord build ()

Returns
AudioRecord a new AudioRecord instance successfully initialized with all the parameters set on this Builder.

Throws
UnsupportedOperationException if the parameters set on the Builder were incompatible, or if they are not supported by the device, or if the device was not available.

setAudioFormat

added in API level 23
AudioRecord.Builder setAudioFormat (AudioFormat format)

Sets the format of the audio data to be captured.

Parameters
format AudioFormat: a non-null AudioFormat instance

This value must never be null.

Returns
AudioRecord.Builder the same Builder instance.

Throws
IllegalArgumentException

setAudioSource

added in API level 23
AudioRecord.Builder setAudioSource (int source)

Parameters
source int: the audio source. See MediaRecorder.AudioSource for the supported audio source definitions.

Returns
AudioRecord.Builder the same Builder instance.

Throws
IllegalArgumentException

setBufferSizeInBytes

added in API level 23
AudioRecord.Builder setBufferSizeInBytes (int bufferSizeInBytes)

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

Returns
AudioRecord.Builder the same Builder instance.

Throws
IllegalArgumentException
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.