public class AudioRecord extends Object implements Closeable
An audio record allocates a native buffer when it is opened. The audio record should be closed with the
close()
method in order to free the native allocation.
While the audio record is open, the native implementation records audio data continuously from the input device and
writes it in the buffer. The readBuffer(byte[], int, int)
method can be used to retrieve and remove a chunk
of audio data from the buffer. If the audio data is not read fast enough by the application, the native
implementation will discard the oldest audio data from the buffer.
Constructor and Description |
---|
AudioRecord(AudioFormat format,
int bufferSize)
Creates and opens an audio record.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this audio record.
|
int |
getBufferSize()
Returns the buffer size of this audio record.
|
AudioFormat |
getFormat()
Returns the format of this audio record.
|
boolean |
isClosed()
Returns whether this audio record is closed.
|
int |
readBuffer(byte[] array,
int offset,
int size)
Reads audio data from the buffer of this audio record.
|
public AudioRecord(AudioFormat format, int bufferSize)
format
- the format.bufferSize
- the buffer size (in bytes).IllegalArgumentException
- if the given buffer size is less than or equal to zero.AudioException
- if the audio record could not be opened.public void close()
This method releases the native resources allocated when opening this audio record. This method does nothing if this audio record is already closed.
close
in interface Closeable
close
in interface AutoCloseable
public int getBufferSize()
public AudioFormat getFormat()
public boolean isClosed()
public int readBuffer(byte[] array, int offset, int size)
This method blocks until the requested size has been read, until this audio record is closed or until the thread is interrupted.
The number of bytes read may be different from the given size if this audio record is closed or if the thread is interrupted.
array
- the array that will contain the data read.offset
- the offset in the array of the first byte read.size
- the maximum number of bytes to read.IndexOutOfBoundsException
- if the given offset or the given size is invalid.