Package ej.audio

Class AudioRecord

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class AudioRecord
    extends Object
    implements Closeable
    Represents an audio recording stream.

    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 Detail

      • AudioRecord

        public AudioRecord​(AudioFormat format,
                           int bufferSize)
        Creates and opens an audio record.
        Parameters:
        format - the format.
        bufferSize - the buffer size (in bytes).
        Throws:
        IllegalArgumentException - if the given buffer size is less than or equal to zero.
        AudioException - if the audio record could not be opened.
        SecurityException - if a security manager exists and does not allow the caller to create an audio record.
    • Method Detail

      • getFormat

        public AudioFormat getFormat()
        Returns the format of this audio record.
        Returns:
        the format.
      • getBufferSize

        public int getBufferSize()
        Returns the buffer size of this audio record.
        Returns:
        the buffer size (in bytes).
      • isClosed

        public boolean isClosed()
        Returns whether this audio record is closed.
        Returns:
        whether this audio record is closed.
      • close

        public void close()
        Closes this audio record.

        This method releases the native resources allocated when opening this audio record. This method does nothing if this audio record is already closed.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
      • readBuffer

        public int readBuffer​(byte[] array,
                              int offset,
                              int size)
        Reads audio data from the buffer of this audio record.

        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.

        Parameters:
        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.
        Returns:
        the number of bytes read.
        Throws:
        IndexOutOfBoundsException - if the given offset or the given size is invalid.
        SecurityException - if a security manager exists and does not allow the caller to read from the buffer.