Most visited

Recently visited

Added in API level 3

AudioRecord

public class AudioRecord
extends Object implements AudioRouting

java.lang.Object
   ↳ android.media.AudioRecord


AudioRecord类管理Java应用程序的音频资源,以便从平台的音频输入硬件录制音频。 这是通过“拉动”(读取)来自AudioRecord对象的数据来实现的。 该应用程序负责轮询使用以下三种方法之一AudioRecord对象在时间: read(byte[], int, int)read(short[], int, int)read(ByteBuffer, int) 选择使用哪种方法将基于AudioRecord用户最方便的音频数据存储格式。

一旦创建,一个AudioRecord对象初始化其关联的音频缓冲区,它将填充新的音频数据。 在构造期间指定的这个缓冲区的大小决定了AudioRecord在尚未读取的“超载”数据之前可以记录多长时间。 数据应该从音频硬件中读取大小小于总记录缓冲区大小的数据块。

Summary

Nested classes

class AudioRecord.Builder

AudioRecord对象的生成器类。

interface AudioRecord.OnRecordPositionUpdateListener

当AudioRecord已达到由setNotificationMarkerPosition(int)设置的通知标记或定期更新记录头进度(如由setPositionNotificationPeriod(int)设置)时要调用的回调的接口定义。

interface AudioRecord.OnRoutingChangedListener

此接口在API级别24中已弃用。用户应改为使用通用型AudioRouting.OnRoutingChangedListener类。

Constants

int ERROR

表示通用操作失败。

int ERROR_BAD_VALUE

表示由于使用无效值而导致的失败。

int ERROR_DEAD_OBJECT

指示报告对象不再有效并需要重新创建的错误代码。

int ERROR_INVALID_OPERATION

表示由于不正确使用方法而导致的失败。

int READ_BLOCKING

指示读取操作的读取模式将被阻塞,直到读取所有请求的数据。

int READ_NON_BLOCKING

指示读取操作的读取模式将在读取尽可能多的音频数据之后立即返回而没有阻塞。

int RECORDSTATE_RECORDING

表示AudioRecord录制状态正在录制

int RECORDSTATE_STOPPED

表示AudioRecord记录状态不记录

int STATE_INITIALIZED

表明AudioRecord状态已准备好被使用

int STATE_UNINITIALIZED

表示AudioRecord状态未成功初始化。

int SUCCESS

表示成功的操作。

Public constructors

AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes)

类构造函数。

Public methods

void addOnRoutingChangedListener(AudioRecord.OnRoutingChangedListener listener, Handler handler)

此方法在API级别24中已弃用。用户应改为使用通用AudioRouting.OnRoutingChangedListener

void addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler)

添加一个 AudioRouting.OnRoutingChangedListener以接收此AudioRecord上路由更改的通知。

int getAudioFormat()

返回配置的音频数据编码。

int getAudioSessionId()

返回音频会话ID。

int getAudioSource()

返回音频记录源。

int getBufferSizeInFrames()

返回本机 AudioRecord缓冲区的 AudioRecord

int getChannelConfiguration()

返回配置的通道位置掩码。

int getChannelCount()

返回配置的通道数量。

AudioFormat getFormat()

返回配置的 AudioRecord格式。

static int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat)

返回成功创建AudioRecord对象所需的最小缓冲区大小(以字节为单位)。

int getNotificationMarkerPosition()

返回以帧为单位表示的通知标记位置。

int getPositionNotificationPeriod()

返回以帧为单位表示的通知更新周期。

AudioDeviceInfo getPreferredDevice()

返回由 setPreferredDevice(AudioDeviceInfo)指定的所选输入。

int getRecordingState()

返回AudioRecord实例的记录状态。

AudioDeviceInfo getRoutedDevice()

返回 AudioDeviceInfo标识此AudioRecord的当前路由。

int getSampleRate()

以Hz为单位返回配置的音频信号采样率。

int getState()

返回AudioRecord实例的状态。

int getTimestamp(AudioTimestamp outTimestamp, int timebase)

按需求投票 AudioTimestamp

int read(short[] audioData, int offsetInShorts, int sizeInShorts, int readMode)

从音频硬件中读取音频数据以便录制成一个短阵列。

int read(ByteBuffer audioBuffer, int sizeInBytes, int readMode)

从音频硬件读取音频数据以便录制到直接缓冲区中。

int read(short[] audioData, int offsetInShorts, int sizeInShorts)

从音频硬件中读取音频数据以便录制成一个短阵列。

int read(float[] audioData, int offsetInFloats, int sizeInFloats, int readMode)

从音频硬件中读取音频数据以便记录到浮点数组中。

int read(byte[] audioData, int offsetInBytes, int sizeInBytes, int readMode)

从音频硬件读取音频数据以便记录到字节数组中。

int read(ByteBuffer audioBuffer, int sizeInBytes)

从音频硬件读取音频数据以便录制到直接缓冲区中。

int read(byte[] audioData, int offsetInBytes, int sizeInBytes)

从音频硬件读取音频数据以便记录到字节数组中。

void release()

释放本机AudioRecord资源。

void removeOnRoutingChangedListener(AudioRecord.OnRoutingChangedListener listener)

此方法在API级别24中已弃用。用户应改为使用通用AudioRouting.OnRoutingChangedListener

void removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener)

删除以前添加的 AudioRouting.OnRoutingChangedListener以接收重新路由通知。

int setNotificationMarkerPosition(int markerInFrames)

如果设置为 setRecordPositionUpdateListener(OnRecordPositionUpdateListener)setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler) ,则设置侦听器被调用的标记位置。

int setPositionNotificationPeriod(int periodInFrames)

设置侦听器被调用的时间段(如果设置为 setRecordPositionUpdateListener(OnRecordPositionUpdateListener)setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler)

boolean setPreferredDevice(AudioDeviceInfo deviceInfo)

指定音频设备(通过 AudioDeviceInfo对象)将输入路由到此AudioRecord。

void setRecordPositionUpdateListener(AudioRecord.OnRecordPositionUpdateListener listener)

设置AudioRecord在达到之前设置的标记或每个周期性记录头位置更新时通知的监听器。

void setRecordPositionUpdateListener(AudioRecord.OnRecordPositionUpdateListener listener, Handler handler)

设置AudioRecord在达到之前设置的标记或每个周期性记录头位置更新时通知的监听器。

void startRecording(MediaSyncEvent syncEvent)

当指定的音频会话发生指定的同步事件时,从AudioRecord实例开始录制。

void startRecording()

从AudioRecord实例开始录制。

void stop()

停止录制。

Protected methods

void finalize()

当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。

Inherited methods

From class java.lang.Object
From interface android.media.AudioRouting

Constants

ERROR

Added in API level 3
int ERROR

表示通用操作失败。

常量值:-1(0xffffffff)

ERROR_BAD_VALUE

Added in API level 3
int ERROR_BAD_VALUE

表示由于使用无效值而导致的失败。

常量值:-2(0xfffffffe)

ERROR_DEAD_OBJECT

Added in API level 24
int ERROR_DEAD_OBJECT

指示报告对象不再有效并需要重新创建的错误代码。

常量值:-6(0xfffffffa)

ERROR_INVALID_OPERATION

Added in API level 3
int ERROR_INVALID_OPERATION

表示由于不正确使用方法而导致的失败。

常量值:-3(0xfffffffd)

READ_BLOCKING

Added in API level 23
int READ_BLOCKING

指示读取操作的读取模式将被阻塞,直到读取所有请求的数据。

常量值:0(0x00000000)

READ_NON_BLOCKING

Added in API level 23
int READ_NON_BLOCKING

指示读取操作的读取模式将在读取尽可能多的音频数据之后立即返回而没有阻塞。

常数值:1(0x00000001)

RECORDSTATE_RECORDING

Added in API level 3
int RECORDSTATE_RECORDING

表示AudioRecord录制状态正在录制

常量值:3(0x00000003)

RECORDSTATE_STOPPED

Added in API level 3
int RECORDSTATE_STOPPED

表示AudioRecord记录状态不记录

常数值:1(0x00000001)

STATE_INITIALIZED

Added in API level 3
int STATE_INITIALIZED

表明AudioRecord状态已准备好被使用

常数值:1(0x00000001)

STATE_UNINITIALIZED

Added in API level 3
int STATE_UNINITIALIZED

表示AudioRecord状态未成功初始化。

常量值:0(0x00000000)

SUCCESS

Added in API level 3
int SUCCESS

表示成功的操作。

常量值:0(0x00000000)

Public constructors

AudioRecord

Added in API level 3
AudioRecord (int audioSource, 
                int sampleRateInHz, 
                int channelConfig, 
                int audioFormat, 
                int bufferSizeInBytes)

类构造函数。 尽管一些无效参数会导致IllegalArgumentException异常,但其他错误不会。 因此,施工后应立即致电getState() ,以确认物体是否可用。

Parameters
audioSource int: the recording source. See MediaRecorder.AudioSource for the recording source definitions.
sampleRateInHz int: the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices. SAMPLE_RATE_UNSPECIFIED means to use a route-dependent value which is usually the sample rate of the source. getSampleRate() can be used to retrieve the actual sample rate chosen.
channelConfig int: describes the configuration of the audio channels. See CHANNEL_IN_MONO and CHANNEL_IN_STEREO. CHANNEL_IN_MONO is guaranteed to work on all devices.
audioFormat int: the format in which the audio data is to be returned. See ENCODING_PCM_8BIT, ENCODING_PCM_16BIT, and ENCODING_PCM_FLOAT.
bufferSizeInBytes int: the total size (in bytes) of the buffer where audio data is written to 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. Using values smaller than getMinBufferSize() will result in an initialization failure.
Throws
IllegalArgumentException

Public methods

addOnRoutingChangedListener

Added in API level 23
void addOnRoutingChangedListener (AudioRecord.OnRoutingChangedListener listener, 
                Handler handler)

此方法在API级别24中已弃用。
用户应该切换到通用AudioRouting.OnRoutingChangedListener

添加 AudioRecord.OnRoutingChangedListener以接收此AudioRecord上路由更改的通知。

Parameters
listener AudioRecord.OnRoutingChangedListener: The AudioRecord.OnRoutingChangedListener interface to receive notifications of rerouting events.
handler Handler: Specifies the Handler object for the thread on which to execute the callback. If null, the Handler associated with the main Looper will be used.

addOnRoutingChangedListener

Added in API level 24
void addOnRoutingChangedListener (AudioRouting.OnRoutingChangedListener listener, 
                Handler handler)

添加 AudioRouting.OnRoutingChangedListener以接收此AudioRecord上路由更改的通知。

Parameters
listener AudioRouting.OnRoutingChangedListener: The AudioRouting.OnRoutingChangedListener interface to receive notifications of rerouting events.
handler Handler: Specifies the Handler object for the thread on which to execute the callback. If null, the Handler associated with the main Looper will be used.

getAudioFormat

Added in API level 3
int getAudioFormat ()

返回配置的音频数据编码。 ENCODING_PCM_8BITENCODING_PCM_16BIT ,并ENCODING_PCM_FLOAT

Returns
int

getAudioSessionId

Added in API level 16
int getAudioSessionId ()

返回音频会话ID。

Returns
int the ID of the audio session this AudioRecord belongs to.

getAudioSource

Added in API level 3
int getAudioSource ()

返回音频记录源。

Returns
int

也可以看看:

getBufferSizeInFrames

Added in API level 23
int getBufferSizeInFrames ()

返回本机AudioRecord缓冲区的AudioRecord 这大于或等于在AudioRecord构造函数或Builder中指定的bufferSizeInBytes转换为帧单位。 本机帧数可能会被放大以适应源创建时的要求,或者如果AudioRecord随后被重新路由。

Returns
int current size in frames of the AudioRecord buffer.
Throws
IllegalStateException

getChannelConfiguration

Added in API level 3
int getChannelConfiguration ()

返回配置的通道位置掩码。

CHANNEL_IN_MONOCHANNEL_IN_STEREO 如果使用通道索引掩码,则此方法可能会返回CHANNEL_INVALID 改为考虑getFormat() ,以获得包含通道位置掩码和通道索引掩码的AudioFormat

Returns
int

getChannelCount

Added in API level 3
int getChannelCount ()

返回配置的通道数量。

Returns
int

getFormat

Added in API level 23
AudioFormat getFormat ()

返回配置的 AudioRecord格式。

Returns
AudioFormat an AudioFormat containing the AudioRecord parameters at the time of configuration.

getMinBufferSize

Added in API level 3
int getMinBufferSize (int sampleRateInHz, 
                int channelConfig, 
                int audioFormat)

返回成功创建AudioRecord对象所需的最小缓冲区大小(以字节为单位)。 请注意,这个大小并不能保证在负载情况下的平滑记录,应根据AudioRecord实例轮询新数据的预期频率来选择较高的值。 有关有效配置值的更多信息,请参阅AudioRecord(int, int, int, int, int)

Parameters
sampleRateInHz int: the sample rate expressed in Hertz. SAMPLE_RATE_UNSPECIFIED is not permitted.
channelConfig int: describes the configuration of the audio channels. See CHANNEL_IN_MONO and CHANNEL_IN_STEREO
audioFormat int: the format in which the audio data is represented. See ENCODING_PCM_16BIT.
Returns
int ERROR_BAD_VALUE if the recording parameters are not supported by the hardware, or an invalid parameter was passed, or ERROR if the implementation was unable to query the hardware for its input properties, or the minimum buffer size expressed in bytes.

也可以看看:

getNotificationMarkerPosition

Added in API level 3
int getNotificationMarkerPosition ()

返回以帧为单位表示的通知标记位置。

Returns
int

getPositionNotificationPeriod

Added in API level 3
int getPositionNotificationPeriod ()

返回以帧为单位表示的通知更新周期。

Returns
int

getPreferredDevice

Added in API level 23
AudioDeviceInfo getPreferredDevice ()

返回由setPreferredDevice(AudioDeviceInfo)指定的所选输入。 请注意,这不能保证与用于录制的实际设备相对应。

Returns
AudioDeviceInfo

getRecordingState

Added in API level 3
int getRecordingState ()

返回AudioRecord实例的记录状态。

Returns
int

也可以看看:

getRoutedDevice

Added in API level 23
AudioDeviceInfo getRoutedDevice ()

返回AudioDeviceInfo标识此AudioRecord的当前路由。 注意:该查询仅在AudioRecord当前正在记录时才有效。 如果不是, getRoutedDevice()将返回null。

Returns
AudioDeviceInfo

getSampleRate

Added in API level 3
int getSampleRate ()

以Hz为单位返回配置的音频信号采样率。 施工后,水槽采样率不会改变。 如果构造函数具有特定的采样率,则采样率就是该值。 如果构造函数具有SAMPLE_RATE_UNSPECIFIED ,则接收器采样率是基于源[sic]的路由相关默认值。

Returns
int

getState

Added in API level 3
int getState ()

返回AudioRecord实例的状态。 在创建AudioRecord实例以检查它是否被正确初始化之后,这很有用。 这确保了获得适当的硬件资源。

Returns
int

也可以看看:

getTimestamp

Added in API level 24
int getTimestamp (AudioTimestamp outTimestamp, 
                int timebase)

按需求投票 AudioTimestamp

AudioTimestamp反映捕获流水线中可用的最早点的帧传递信息。

调用 startRecording()下一个 stop()将帧计数重置为0。

Parameters
outTimestamp AudioTimestamp: a caller provided non-null AudioTimestamp instance, which is updated with the AudioRecord frame delivery information upon success.
timebase int: one of AudioTimestamp.TIMEBASE_BOOTTIME or AudioTimestamp.TIMEBASE_MONOTONIC, used to select the clock for the AudioTimestamp time.
Returns
int SUCCESS if a timestamp is available, or ERROR_INVALID_OPERATION if a timestamp not available.

read

Added in API level 23
int read (short[] audioData, 
                int offsetInShorts, 
                int sizeInShorts, 
                int readMode)

从音频硬件中读取音频数据以便录制成一个短阵列。 在AudioRecord构造函数中指定的格式应该是ENCODING_PCM_16BIT以对应数组中的数据。

Parameters
audioData short: the array to which the recorded audio data is written.
offsetInShorts int: index in audioData from which the data is written expressed in shorts. Must not be negative, or cause the data access to go out of bounds of the array.
sizeInShorts int: the number of requested shorts. Must not be negative, or cause the data access to go out of bounds of the array.
readMode int: one of READ_BLOCKING, READ_NON_BLOCKING.
With READ_BLOCKING, the read will block until all the requested data is read.
With READ_NON_BLOCKING, the read will return immediately after reading as much audio data as possible without blocking.
Returns
int zero or the positive number of shorts that were read, or one of the following error codes. The number of shorts will be a multiple of the channel count not to exceed sizeInShorts.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

read

Added in API level 23
int read (ByteBuffer audioBuffer, 
                int sizeInBytes, 
                int readMode)

从音频硬件读取音频数据以便录制到直接缓冲区中。 如果此缓冲区不是直接缓冲区,则此方法将始终返回0.请注意,在调用此方法后,此缓冲区上由position()返回的值保持不变。 数据在缓冲区中的表示形式将取决于AudioRecord构造函数中指定的格式,并且将是本机排序。

Parameters
audioBuffer ByteBuffer: the direct buffer to which the recorded audio data is written. Data is written to audioBuffer.position().
sizeInBytes int: the number of requested bytes. It is recommended but not enforced that the number of bytes requested be a multiple of the frame size (sample size in bytes multiplied by the channel count).
readMode int: one of READ_BLOCKING, READ_NON_BLOCKING.
With READ_BLOCKING, the read will block until all the requested data is read.
With READ_NON_BLOCKING, the read will return immediately after reading as much audio data as possible without blocking.
Returns
int zero or the positive number of bytes that were read, or one of the following error codes. The number of bytes will not exceed sizeInBytes and will be truncated to be a multiple of the frame size.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

read

Added in API level 3
int read (short[] audioData, 
                int offsetInShorts, 
                int sizeInShorts)

从音频硬件中读取音频数据以便录制成一个短阵列。 在AudioRecord构造函数中指定的格式应该是ENCODING_PCM_16BIT以对应数组中的数据。

Parameters
audioData short: the array to which the recorded audio data is written.
offsetInShorts int: index in audioData to which the data is written expressed in shorts. Must not be negative, or cause the data access to go out of bounds of the array.
sizeInShorts int: the number of requested shorts. Must not be negative, or cause the data access to go out of bounds of the array.
Returns
int zero or the positive number of shorts that were read, or one of the following error codes. The number of shorts will be a multiple of the channel count not to exceed sizeInShorts.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

read

Added in API level 23
int read (float[] audioData, 
                int offsetInFloats, 
                int sizeInFloats, 
                int readMode)

从音频硬件中读取音频数据以便记录到浮点数组中。 AudioRecord构造函数中指定的格式应该是ENCODING_PCM_FLOAT以对应数组中的数据。

Parameters
audioData float: the array to which the recorded audio data is written.
offsetInFloats int: index in audioData from which the data is written. Must not be negative, or cause the data access to go out of bounds of the array.
sizeInFloats int: the number of requested floats. Must not be negative, or cause the data access to go out of bounds of the array.
readMode int: one of READ_BLOCKING, READ_NON_BLOCKING.
With READ_BLOCKING, the read will block until all the requested data is read.
With READ_NON_BLOCKING, the read will return immediately after reading as much audio data as possible without blocking.
Returns
int zero or the positive number of floats that were read, or one of the following error codes. The number of floats will be a multiple of the channel count not to exceed sizeInFloats.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

read

Added in API level 23
int read (byte[] audioData, 
                int offsetInBytes, 
                int sizeInBytes, 
                int readMode)

从音频硬件读取音频数据以便记录到字节数组中。 在AudioRecord构造函数中指定的格式应该是ENCODING_PCM_8BIT以对应数组中的数据。 格式可以是ENCODING_PCM_16BIT ,但不推荐使用。

Parameters
audioData byte: the array to which the recorded audio data is written.
offsetInBytes int: index in audioData to which the data is written expressed in bytes. Must not be negative, or cause the data access to go out of bounds of the array.
sizeInBytes int: the number of requested bytes. Must not be negative, or cause the data access to go out of bounds of the array.
readMode int: one of READ_BLOCKING, READ_NON_BLOCKING.
With READ_BLOCKING, the read will block until all the requested data is read.
With READ_NON_BLOCKING, the read will return immediately after reading as much audio data as possible without blocking.
Returns
int zero or the positive number of bytes that were read, or one of the following error codes. The number of bytes will be a multiple of the frame size in bytes not to exceed sizeInBytes.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

read

Added in API level 3
int read (ByteBuffer audioBuffer, 
                int sizeInBytes)

从音频硬件读取音频数据以便录制到直接缓冲区中。 如果此缓冲区不是直接缓冲区,则此方法将始终返回0.请注意,调用此方法后,此缓冲区上由position()返回的值保持不变。 数据在缓冲区中的表示形式将取决于AudioRecord构造函数中指定的格式,并且将是本机排序。

Parameters
audioBuffer ByteBuffer: the direct buffer to which the recorded audio data is written. Data is written to audioBuffer.position().
sizeInBytes int: the number of requested bytes. It is recommended but not enforced that the number of bytes requested be a multiple of the frame size (sample size in bytes multiplied by the channel count).
Returns
int zero or the positive number of bytes that were read, or one of the following error codes. The number of bytes will not exceed sizeInBytes and will be truncated to be a multiple of the frame size.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

read

Added in API level 3
int read (byte[] audioData, 
                int offsetInBytes, 
                int sizeInBytes)

从音频硬件读取音频数据以便记录到字节数组中。 在AudioRecord构造函数中指定的格式应该是ENCODING_PCM_8BIT以对应数组中的数据。

Parameters
audioData byte: the array to which the recorded audio data is written.
offsetInBytes int: index in audioData from which the data is written expressed in bytes.
sizeInBytes int: the number of requested bytes.
Returns
int zero or the positive number of bytes that were read, or one of the following error codes. The number of bytes will not exceed sizeInBytes.
  • ERROR_INVALID_OPERATION if the object isn't properly initialized
  • ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes
  • ERROR_DEAD_OBJECT if the object is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next read()
  • ERROR in case of other error

release

Added in API level 3
void release ()

释放本机AudioRecord资源。 该对象不能再使用,并且在调用release()之后应将该引用设置为null。

removeOnRoutingChangedListener

Added in API level 23
void removeOnRoutingChangedListener (AudioRecord.OnRoutingChangedListener listener)

此方法在API级别24中已弃用。
用户应改用通用AudioRouting.OnRoutingChangedListener

删除以前添加的 AudioRecord.OnRoutingChangedListener以接收重新路由通知。

Parameters
listener AudioRecord.OnRoutingChangedListener: The previously added AudioRecord.OnRoutingChangedListener interface to remove.

removeOnRoutingChangedListener

Added in API level 24
void removeOnRoutingChangedListener (AudioRouting.OnRoutingChangedListener listener)

删除以前添加的 AudioRouting.OnRoutingChangedListener以接收重新路由通知。

Parameters
listener AudioRouting.OnRoutingChangedListener: The previously added AudioRouting.OnRoutingChangedListener interface to remove.

setNotificationMarkerPosition

Added in API level 3
int setNotificationMarkerPosition (int markerInFrames)

如果设置为 setRecordPositionUpdateListener(OnRecordPositionUpdateListener)setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler) ,则设置侦听器被调用的标记位置。

Parameters
markerInFrames int: marker position expressed in frames
Returns
int error code or success, see SUCCESS, ERROR_BAD_VALUE, ERROR_INVALID_OPERATION

setPositionNotificationPeriod

Added in API level 3
int setPositionNotificationPeriod (int periodInFrames)

设置侦听程序的调用周期(如果设置为setRecordPositionUpdateListener(OnRecordPositionUpdateListener)setRecordPositionUpdateListener(OnRecordPositionUpdateListener, Handler) 如果周期太短,通知可能会丢失。

Parameters
periodInFrames int: update period expressed in frames
Returns
int error code or success, see SUCCESS, ERROR_INVALID_OPERATION

setPreferredDevice

Added in API level 23
boolean setPreferredDevice (AudioDeviceInfo deviceInfo)

指定一个音频设备(通过 AudioDeviceInfo对象)将输入路由到此AudioRecord。

Parameters
deviceInfo AudioDeviceInfo: The AudioDeviceInfo specifying the audio source. If deviceInfo is null, default routing is restored.
Returns
boolean true if successful, false if the specified AudioDeviceInfo is non-null and does not correspond to a valid audio input device.

setRecordPositionUpdateListener

Added in API level 3
void setRecordPositionUpdateListener (AudioRecord.OnRecordPositionUpdateListener listener)

设置AudioRecord在达到之前设置的标记或每个周期性记录头位置更新时通知的监听器。

setRecordPositionUpdateListener

Added in API level 3
void setRecordPositionUpdateListener (AudioRecord.OnRecordPositionUpdateListener listener, 
                Handler handler)

设置AudioRecord在达到之前设置的标记或每个周期性记录头位置更新时通知的监听器。 使用此方法在与除创建AudioRecord实例之外的另一个线程关联的Handler中接收AudioRecord事件。

Parameters
handler Handler: the Handler that will receive the event notification messages.

startRecording

Added in API level 16
void startRecording (MediaSyncEvent syncEvent)

当指定的音频会话发生指定的同步事件时,从AudioRecord实例开始录制。

Parameters
syncEvent MediaSyncEvent: event that triggers the capture.
Throws
IllegalStateException
IllegalStateException

也可以看看:

startRecording

Added in API level 3
void startRecording ()

从AudioRecord实例开始录制。

Throws
IllegalStateException

stop

Added in API level 3
void stop ()

停止录制。

Throws
IllegalStateException

Protected methods

finalize

Added in API level 3
void finalize ()

当垃圾收集确定没有更多对该对象的引用时,由对象上的垃圾回收器调用。 子类会覆盖finalize方法以处置系统资源或执行其他清理。

的常规协定finalize是,它被调用,如果当在Java TM虚拟机已确定不再有由该目的可以通过还没有死亡,除了作为一个动作的结果的任何线程访问的任何手段取决于某些其他可以完成的对象或类别的最终定稿。 方法finalize可以采取任何行动,包括使这个对象再次可用于其他线程; 然而, finalize的通常目的是在对象被不可撤销地丢弃之前执行清理操作。 例如,表示输入/输出连接的对象的finalize方法可能会执行显式I / O事务,以在永久丢弃该对象之前中断连接。

Objectfinalize方法Object执行特别的操作; 它只是正常返回。 Object子类可能会覆盖此定义。

Java编程语言不保证哪个线程将为任何给定的对象调用finalize方法。 但是,保证调用finalize的线程在调用finalize时不会保留任何用户可见的同步锁。 如果finalize方法引发未捕获的异常,则忽略该异常,并终止该对象的终止。

在针对某个对象调用 finalize方法之后,在Java虚拟机再次确定不再有任何方法可以通过尚未死亡的任何线程访问此对象,包括可能的操作通过准备完成的其他对象或类别,此时该对象可能被丢弃。

对于任何给定的对象,Java虚拟机从不会多次调用 finalize方法。

finalize方法抛出的任何异常 finalize导致此对象的终止被暂停,但是会被忽略。

Hooray!