Most visited

Recently visited

Added in API level 23

AudioRecord.Builder

public static class AudioRecord.Builder
extends Object

java.lang.Object
    android.media.AudioRecord.Builder


AudioRecord对象的生成器类。 使用这个类来配置和创建一个AudioRecord实例。 通过设置录音源和音频格式参数,可以指出哪些参数与设备的默认行为不同。

以下是一个示例,其中 Builder用于指定所有 AudioFormat参数,以供新的 AudioRecord实例使用:

 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();
 

如果音频源未使用setAudioSource(int)设置,则使用DEFAULT
如果音频格式未指定或不完整,其通道配置将为CHANNEL_IN_MONO ,编码将为ENCODING_PCM_16BIT 采样率取决于实际选择捕获的设备,可以用getSampleRate()方法查询。
如果未使用setBufferSizeInBytes(int)指定缓冲区大小,则使用源的最小缓冲区大小。

摘要(Summary)

Public constructors

AudioRecord.Builder()

使用上述默认值构造一个新的Builder。

公共方法(Public methods)

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

设置要捕捉的音频数据的格式。

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

设置录制过程中写入音频数据的缓冲区的总大小(以字节为单位)。

继承方法(Inherited methods)

From class java.lang.Object

Public constructors

AudioRecord.Builder

Added in API level 23
AudioRecord.Builder ()

使用上述默认值构造一个新的Builder。

公共方法(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)

设置要捕捉的音频数据的格式。

参数(Parameters)
format AudioFormat: a non-null AudioFormat instance
返回(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)

设置录制过程中写入音频数据的缓冲区的总大小(以字节为单位)。 新的音频数据可以以比这个尺寸更小的块读取。 请参阅getMinBufferSize(int, int, int)以确定成功创建AudioRecord实例所需的最小缓冲区大小。 由于bufferSizeInBytes可能会在内部增加以适应源需求,因此请使用getBufferSizeInFrames()来确定帧中的实际缓冲区大小。

参数(Parameters)
bufferSizeInBytes int: a value strictly greater than 0
返回(Returns)
AudioRecord.Builder the same Builder instance.
抛出异常(Throws)
IllegalArgumentException

Hooray!