public class AudioCapturer extends Object
This class provides the record and stop interfaces to record and pause PCM audio.
The initial parameters of this class are included in the AudioCapturerInfo class.
SystemCapability.Multimedia#AUDIO| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
AudioCapturer.State |
Describes recording states.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
CAPTURER_SESSION_ID_NONE |
Indicates the default ID when there is no recording session.
|
static int |
ERROR |
Indicates that the operation has failed.
|
static int |
ERROR_BAD_VALUE |
Indicates that the failure is caused by an invalid value.
|
static int |
ERROR_DEAD_OBJECT |
Indicates that the native layer is unavailable.
|
static int |
ERROR_INVALID_OPERATION |
Indicates that the failure is caused by the use of an incorrect method.
|
static int |
INPUT_DEVICE_ID_NONE |
Indicates the default ID when there is no input device.
|
static int |
SUCCESS |
Indicates that the operation is successful.
|
| 构造器 | 说明 |
|---|---|
AudioCapturer(AudioCapturerInfo audioCapturerInfo) |
A constructor used to create an
AudioCapturerInfo instance with specified information about the audio
streams to record. |
AudioCapturer(AudioCapturerInfo audioCapturerInfo,
AudioDeviceDescriptor devInfo) |
A constructor used to create an
AudioCapturerInfo instance with specified information about the audio
streams and input device. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
addSoundEffect(UUID type,
String packageName) |
Adds a sound effect for audio recording.
|
AudioStreamInfo.ChannelMask |
getAudioChannel() |
Obtains channel mask information used for audio recording.
|
int |
getAudioInputSource() |
Obtains the audio stream input source.
|
AudioStreamInfo |
getAudioStreamInfo() |
Obtains an
AudioStreamInfo instance of an audio source. |
boolean |
getAudioTime(Timestamp timestamp,
Timestamp.Timebase timebase) |
Obtains a
Timestamp on demand. |
int |
getBufferFrameCount() |
Obtains the buffer size in frames.
|
int |
getCapturerSessionId() |
Obtains a recording session ID.
|
int |
getChannelCount() |
Obtains the number of audio recording channels.
|
AudioDeviceDescriptor |
getCurrentDevice() |
Obtains information about the device that is recording audio.
|
AudioStreamInfo.EncodingFormat |
getEncodingFormat() |
Obtains the audio encoding format for the recording.
|
static int |
getMinBufferSize(int sampleRate,
int channelCount,
int audioFormat) |
Obtains the minimum buffer size (in bytes) required in the specified conditions.
|
int |
getSampleRate() |
Obtains the audio stream sampling rate.
|
AudioDeviceDescriptor |
getSelectedDevice() |
Obtains information about the audio input device.
|
Set<SoundEffect> |
getSoundEffects() |
Obtains activated sound effects.
|
AudioCapturer.State |
getState() |
Obtains the audio capture state.
|
int |
read(byte[] data,
int offset,
int size) |
Reads audio data from the audio hardware.
|
int |
read(byte[] data,
int offset,
int size,
boolean isBlocking) |
Reads audio data from the recording hardware device and writes the data into a byte array.
|
int |
read(float[] data,
int offsetInFloats,
int sizeInFloats) |
(Blocking method) Reads audio data from the recording hardware device and writes the data into a float array.
|
int |
read(float[] data,
int offsetInFloats,
int sizeInFloats,
boolean isBlocking) |
Reads audio data from the recording hardware device to a float array.
|
int |
read(short[] data,
int offsetInShorts,
int sizeInShorts) |
(Blocking method) Reads audio data from the recording hardware device and writes the data into a short array.
|
int |
read(short[] data,
int offsetInShorts,
int sizeInShorts,
boolean isBlocking) |
Reads audio data from the recording hardware device and writes the data into a short array.
|
int |
read(ByteBuffer buffer,
int sizeInBytes) |
(Blocking method) Reads audio data from the recording hardware device and writes the data into the direct buffer.
|
int |
read(ByteBuffer buffer,
int sizeInBytes,
boolean isBlocking) |
Reads audio data from the recording hardware device and writes the data into the direct buffer.
|
boolean |
release() |
Releases local audio recording object.
|
void |
setFrameIntervalObserver(FrameIntervalObserver observer,
int interval,
EventHandler handler) |
Sets an observer to listen for the event that the recording progress reaches a multiple of
interval. |
void |
setFramePositionObserver(FramePositionObserver observer,
int position,
EventHandler handler) |
Sets an observer to listen for the event that the recording progress reaches a specified position.
|
boolean |
start() |
Starts audio recording.
|
boolean |
stop() |
Stops audio recording.
|
public static final int CAPTURER_SESSION_ID_NONE
public static final int ERROR
public static final int ERROR_BAD_VALUE
public static final int ERROR_DEAD_OBJECT
public static final int ERROR_INVALID_OPERATION
public static final int INPUT_DEVICE_ID_NONE
public static final int SUCCESS
public AudioCapturer(AudioCapturerInfo audioCapturerInfo, AudioDeviceDescriptor devInfo) throws IllegalArgumentException
AudioCapturerInfo instance with specified information about the audio
streams and input device.audioCapturerInfo - Indicates information about the audio streams to record.devInfo - Indicates information about the preference input device of this AudioCapturer.IllegalArgumentException - Throws this exception if a parameter is invalid.public AudioCapturer(AudioCapturerInfo audioCapturerInfo) throws IllegalArgumentException
AudioCapturerInfo instance with specified information about the audio
streams to record.audioCapturerInfo - Indicates information about the audio streams to record.IllegalArgumentException - Throws this exception if a parameter is invalid.public static int getMinBufferSize(int sampleRate,
int channelCount,
int audioFormat)
sampleRate - Indicates the sampling rate (Hz).channelCount - Indicates the audio channel count.audioFormat - Indicates the audio data format.-1 if an exception occurs.public boolean addSoundEffect(UUID type, String packageName)
type - Indicates a sound effect type, which can be SoundEffect#SOUND_EFFECT_TYPE_EC,
SoundEffect#SOUND_EFFECT_TYPE_GC, and SoundEffect#SOUND_EFFECT_TYPE_NS.packageName - Indicates an application package name.true if the sound effect is added; returns false otherwise.public Set<SoundEffect> getSoundEffects()
public int getCapturerSessionId()
public AudioDeviceDescriptor getSelectedDevice()
This method is used to obtain the input device information specified using the AudioCapturer(ohos.media.audio.AudioCapturerInfo) method.
The input device may be different from the recording device.
public AudioDeviceDescriptor getCurrentDevice()
This method is used to obtain the audio recording device configured using the
AudioCapturer#AudioCapturer(AudioCapturerInfo, AudioDeviceDescriptor) method.
null otherwise.public AudioCapturer.State getState()
State.public AudioStreamInfo.EncodingFormat getEncodingFormat()
public int getChannelCount()
public int getSampleRate()
public int getBufferFrameCount()
public int getAudioInputSource()
AudioCapturerInfo.AudioInputSource.public AudioStreamInfo.ChannelMask getAudioChannel()
AudioStreamInfo.ChannelMask object.public boolean getAudioTime(Timestamp timestamp, Timestamp.Timebase timebase)
Timestamp on demand.
The Timestamp reflects the frame delivery information at the earliest point available in the capture pipeline.
timestamp - a caller provided non-null Timestamp instance.timebase - one of
Timestamp.Timebase#BOOTTIME or
Timestamp.Timebase#MONOTONIC,
used to select the clock for the Timestamp time.true if the timestamp get successfully; returns false otherwise.public AudioStreamInfo getAudioStreamInfo()
AudioStreamInfo instance of an audio source.AudioStreamInfo instance set during the construction of this AudioCapturer.public final boolean start()
true if audio recording is started successfully; returns false otherwise.public int read(byte[] data,
int offset,
int size)
data - Indicates the audio data.offset - Indicates the offset (in bytes) of the audio data.size - Indicates the size (in bytes) of the audio to be read.size) of the audio data if the data is
read successfully; returns one of the following error codes otherwise:
ERROR_BAD_VALUE indicates a parameter error.
ERROR_INVALID_OPERATION indicates that the AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT indicates that the AudioCapturer instance is abnormal.public int read(byte[] data,
int offset,
int size,
boolean isBlocking)
data - Indicates the byte array into which the audio data to be written.offset - Indicates the offsets (in bytes) for writing the audio data.size - Indicates the size (in bytes) of the audio data to be read.isBlocking - Specifies whether this method blocks until all the requested data is read.0 to size;
returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public int read(short[] data,
int offsetInShorts,
int sizeInShorts)
data - Indicates the short array into which the audio data to be written.offsetInShorts - Indicates the offsets (represented by shorts) for writing the audio data.sizeInShorts - Indicates the size (represented by shorts) of the audio data to be read.0 to
sizeInShorts; returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public int read(short[] data,
int offsetInShorts,
int sizeInShorts,
boolean isBlocking)
data - Indicates the short array into which the audio data to be written.offsetInShorts - Indicates the offsets (represented by shorts) for writing the audio data.sizeInShorts - Indicates the size (represented by shorts) of the audio data to be read.isBlocking - Specifies whether this method blocks until all the requested data is read.0 to
sizeInShorts; returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public int read(float[] data,
int offsetInFloats,
int sizeInFloats)
data - Indicates the float array into which the audio data to be written.offsetInFloats - Indicates the offsets (represented by floats) for writing the audio data.sizeInFloats - Indicates the size (represented by floats) of the audio data to be read.0 to
sizeInFloats; returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public int read(float[] data,
int offsetInFloats,
int sizeInFloats,
boolean isBlocking)
data - Indicates the float array into which the audio data to be written.offsetInFloats - Indicates the offsets (represented by floats) for writing the audio data.sizeInFloats - Indicates the size (represented by floats) of the audio data to be read.isBlocking - Specifies whether this method blocks until all the requested data is read.0 to
sizeInFloats; returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public int read(ByteBuffer buffer, int sizeInBytes)
0.buffer - Indicates the direct buffer where the audio data to be written.sizeInBytes - Indicates the size (in bytes) of the audio data to be read. Set the size to an integral
multiple of the frame size.0 to
sizeInBytes; returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public int read(ByteBuffer buffer, int sizeInBytes, boolean isBlocking)
0.buffer - Indicates the direct buffer where the audio data to be written.sizeInBytes - Indicates the size (in bytes) of the audio data to be read. Set the size to an integral
multiple of the frame size.isBlocking - Specifies whether this method blocks until all the requested data is read.0 to
sizeInBytes; returns one of the following error codes otherwise:
ERROR_BAD_VALUE: An input parameter is incorrect.
ERROR_INVALID_OPERATION: The AudioCapturer instance is not initialized.
ERROR_DEAD_OBJECT: The status of the AudioCapturer instance is abnormal.public final boolean stop()
true if audio recording is stopped successfully; returns false otherwise.public final boolean release()
true if local audio is released successfully; returns false otherwise.public void setFramePositionObserver(FramePositionObserver observer, int position, EventHandler handler)
observer - Indicates the observer to set.position - Indicates the recording position.handler - Indicates the event handler to invoke the observer. If you do not specify a handler, the
default one will be used.public void setFrameIntervalObserver(FrameIntervalObserver observer, int interval, EventHandler handler)
interval.observer - Indicates the observer to set.interval - Indicates the audio frame interval.handler - Indicates the event handler to invoke the observer. If you do not specify a handler, the
default one will be used.