Class AudioSink
- java.lang.Object
-
- ej.audio.AudioSink
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class AudioSink extends Object implements Closeable
Represents an audio playback stream.An audio sink allocates a native buffer when it is opened. The audio sink should be closed with the
close()method in order to free the native allocation.While the audio sink is open, the native implementation reads audio data continuously from the buffer and plays it on the output device. The
writeBuffer(byte[], int, int)method can be used to write a chunk of audio data in the buffer. If audio data is not written fast enough by the application, the output device may play undesired silences. ThewaitForBufferFlushed()method can be used to wait until all the audio data written in the buffer has been played back.The volume of the playback can be configured by calling
setVolume(float).
-
-
Constructor Summary
Constructors Constructor Description AudioSink(AudioFormat format, int bufferSize)Creates and opens an audio sink.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this audio sink.intgetBufferSize()Returns the buffer size of this audio sink.AudioFormatgetFormat()Returns the format of this audio sink.booleanisClosed()Returns whether this audio sink is closed.voidsetVolume(float gain)Sets the volume of this audio sink.booleanwaitForBufferFlushed()Waits for the buffer of this audio sink to be flushed.intwriteBuffer(byte[] array, int offset, int size)Writes audio data in the buffer of this audio sink.
-
-
-
Constructor Detail
-
AudioSink
public AudioSink(AudioFormat format, int bufferSize)
Creates and opens an audio sink.- 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 sink could not be opened.SecurityException- if a security manager exists and does not allow the caller to create an audio sink.
-
-
Method Detail
-
getFormat
public AudioFormat getFormat()
Returns the format of this audio sink.- Returns:
- the format.
-
getBufferSize
public int getBufferSize()
Returns the buffer size of this audio sink.- Returns:
- the buffer size (in bytes).
-
isClosed
public boolean isClosed()
Returns whether this audio sink is closed.- Returns:
- whether this audio sink is closed.
-
close
public void close()
Closes this audio sink.This method releases the native resources allocated when opening this audio sink. This method does nothing if this audio sink is already closed.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
setVolume
public void setVolume(float gain)
Sets the volume of this audio sink.- Parameters:
gain- the gain (between0.0fand1.0f).
-
writeBuffer
public int writeBuffer(byte[] array, int offset, int size)Writes audio data in the buffer of this audio sink.This method blocks until the requested size has been written, until this audio sink is closed or until the thread is interrupted.
The number of bytes written may be different from the given size if this audio sink is closed or until the thread is interrupted.
- Parameters:
array- the array containing the data to write.offset- the offset in the array of the first byte to write.size- the maximum number of bytes to write.- Returns:
- the number of bytes written.
- 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 write in the buffer.
-
waitForBufferFlushed
public boolean waitForBufferFlushed()
Waits for the buffer of this audio sink to be flushed.This method blocks until the buffer is empty (until all the audio data has been played back), until this audio sink is closed or until the thread is interrupted.
- Returns:
trueif the buffer is empty,falseif this audio sink is closed or the thread is interrupted.- Throws:
SecurityException- if a security manager exists and does not allow the caller to wait for the buffer.
-
-