public static class AudioRecord.Builder extends Object
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)
,
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 AudioRecord.getSampleRate()
method.
If the buffer size is not specified with setBufferSizeInBytes(int)
,
the minimum buffer size for the source is used.
Constructor and Description |
---|
Builder()
Constructs a new Builder with the default values as described above.
|
Modifier and Type | Method and Description |
---|---|
AudioRecord |
build() |
AudioRecord.Builder |
setAudioAttributes(AudioAttributes attributes) |
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.
|
AudioRecord.Builder |
setSessionId(int sessionId) |
public Builder()
public AudioRecord.Builder setAudioSource(int source) throws IllegalArgumentException
source
- the audio source.
See MediaRecorder.AudioSource
for the supported audio source definitions.IllegalArgumentException
public AudioRecord.Builder setAudioAttributes(AudioAttributes attributes) throws IllegalArgumentException
attributes
- a non-null AudioAttributes
instance that contains the capture
preset to be used.IllegalArgumentException
public AudioRecord.Builder setAudioFormat(AudioFormat format) throws IllegalArgumentException
format
- a non-null AudioFormat
instanceIllegalArgumentException
public AudioRecord.Builder setBufferSizeInBytes(int bufferSizeInBytes) throws IllegalArgumentException
AudioRecord.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 AudioRecord.getBufferSizeInFrames()
to determine the actual buffer size
in frames.bufferSizeInBytes
- a value strictly greater than 0IllegalArgumentException
public AudioRecord.Builder setSessionId(int sessionId) throws IllegalArgumentException
sessionId
- ID of audio session the AudioRecord must be attached to, or
AudioManager.AUDIO_SESSION_ID_GENERATE
if the session isn't known at
construction time.IllegalArgumentException
public AudioRecord build() throws UnsupportedOperationException
AudioRecord
instance successfully initialized with all
the parameters set on this Builder
.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.