Skip to content

Most visited

Recently visited

navigation

I2sDevice

I2sDevice

public class I2sDevice
extends Object implements Closeable

java.lang.Object
   ↳ com.google.android.things.pio.I2sDevice


Controls an I2S device. Opening an I2S device takes ownership of it for the whole system, preventing anyone else from opening/accessing this device until you call close(). Forgetting to call close() will prevent anyone (including the same process/app) from using the device.

Summary

Constants

int FLAG_DIRECTION_IN

Flag used to specify a device supports input

int FLAG_DIRECTION_OUT

Flag used to specify a device supports output

int PCM_FORMAT_16_BIT

Constant used to specify signed 16-bit little-endian PCM

int PCM_FORMAT_24_BIT

Constant used to specify signed 24-bit little-endian PCM

int PCM_FORMAT_32_BIT

Constant used to specify signed 32-bit little-endian PCM

int PCM_FORMAT_8_BIT

Constant used to specify signed 8-bit PCM

Public methods

void close()

Close and release the device.

boolean getInputTimestamp(AudioTimestamp timestamp)

Get the timestamp when a recent audio sample entered the kernel.

String getName()

Returns the I/O name.

boolean getOutputTimestamp(AudioTimestamp timestamp)

Get the timestamp when a recent audio sample entered the kernel.

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

Read raw audio in from the audio device.

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

Read raw audio in from the audio device.

int read(ByteBuffer buffer, int sizeInBytes)

Read raw audio in from the audio device.

int write(ByteBuffer buffer, int sizeInBytes)

Write raw audio to the audio device.

int write(byte[] buffer, int offsetInBytes, int sizeInBytes)

Write raw audio to the audio device.

int write(short[] buffer, int offsetInShorts, int sizeInShorts)

Write raw audio to the audio device.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

FLAG_DIRECTION_IN

int FLAG_DIRECTION_IN

Flag used to specify a device supports input

Constant Value: 1 (0x00000001)

FLAG_DIRECTION_OUT

int FLAG_DIRECTION_OUT

Flag used to specify a device supports output

Constant Value: 2 (0x00000002)

PCM_FORMAT_16_BIT

int PCM_FORMAT_16_BIT

Constant used to specify signed 16-bit little-endian PCM

Constant Value: 2 (0x00000002)

PCM_FORMAT_24_BIT

int PCM_FORMAT_24_BIT

Constant used to specify signed 24-bit little-endian PCM

Constant Value: 3 (0x00000003)

PCM_FORMAT_32_BIT

int PCM_FORMAT_32_BIT

Constant used to specify signed 32-bit little-endian PCM

Constant Value: 4 (0x00000004)

PCM_FORMAT_8_BIT

int PCM_FORMAT_8_BIT

Constant used to specify signed 8-bit PCM

Constant Value: 1 (0x00000001)

Public methods

close

void close ()

Close and release the device. Must be called in order to make the device available to other users.

Throws
IOException The I2sDevice is already closed.

getInputTimestamp

boolean getInputTimestamp (AudioTimestamp timestamp)

Get the timestamp when a recent audio sample entered the kernel. The timestamp received maches a specific sample with the timestamp when it was received. The sample given is not guaranteed to be the most recent sample received in the kernel, but it will be returned as best-effort with respect to freshness.

Parameters
timestamp AudioTimestamp: A reference to a non-null AudioTimestamp instance allocated and owned by caller.
Returns
boolean true if timestamp is available, false if no timestamp is available. If a timestamp if available, the AudioTimestamp instance is filled in with a position in frame units, together with the estimated time when that frame was presented or is committed to be presented.
Throws
IOException if there was a major error such as failure to talk to the I2S device.

getName

String getName ()

Returns the I/O name.

Returns
String

getOutputTimestamp

boolean getOutputTimestamp (AudioTimestamp timestamp)

Get the timestamp when a recent audio sample entered the kernel. The timestamp received maches a specific sample with the timestamp when it was received. The sample given is not guaranteed to be the most recent sample received in the kernel, but it will be returned as best-effort with respect to freshness.

Parameters
timestamp AudioTimestamp: A reference to a non-null AudioTimestamp instance allocated and owned by caller.
Returns
boolean true if timestamp is available, false if no timestamp is available. If a timestamp if available, the AudioTimestamp instance is filled in with a position in frame units, together with the estimated time when that frame was presented or is committed to be presented.
Throws
IOException if there was a major error such as failure to talk to the I2S device.

read

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

Read raw audio in from the audio device. This call blocks until the entire operation is completed.

Parameters
buffer short: Buffer to read into.
offsetInShorts int: Offset into the supplied array to begin storing output.
sizeInShorts int: Number of shorts to read. Offset + size may not exceed buffer length.
Returns
int The number of shorts read.
Throws
IOException If there was an error reading from the device.
IllegalArgumentException If offset or size could lead to array access out of bounds.

read

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

Read raw audio in from the audio device. This call blocks until the entire operation is completed.

Parameters
buffer byte: Buffer to read into.
offsetInBytes int: Offset into the supplied array to begin storing output.
sizeInBytes int: Number of bytes to read. Offset + size may not be larger than the buffer.
Returns
int The number of bytes read.
Throws
IOException If there was an error reading from the device.
IllegalArgumentException If offset or size could lead to array access out of bounds.

read

int read (ByteBuffer buffer, 
                int sizeInBytes)

Read raw audio in from the audio device. This call blocks until the entire operation is completed.

Parameters
buffer ByteBuffer: Buffer to read into.
sizeInBytes int: Number of bytes to read. It is recommended but not enforced that the number of bytes requested is a multiple of the frame size (sample size in bytes multiplied by the channel count).
Returns
int The number of bytes read.
Throws
IOException If there was an error reading from the device.
IllegalArgumentException If offset or size could lead to array access out of bounds.

write

int write (ByteBuffer buffer, 
                int sizeInBytes)

Write raw audio to the audio device. This call blocks until the entire operation is completed.

Parameters
buffer ByteBuffer: Buffer to write out of. Must have a backing array.
sizeInBytes int: Number of bytes to write. It is recommended but not enforced that the number of bytes requested is a multiple of the frame size (sample size in bytes multiplied by the channel count).
Returns
int The number of bytes written.
Throws
IOException If there was an error writing to the device.
IllegalArgumentException If offset or size could lead to array access out of bounds.

write

int write (byte[] buffer, 
                int offsetInBytes, 
                int sizeInBytes)

Write raw audio to the audio device. This call blocks until the entire operation is completed.

Parameters
buffer byte: Data to write.
offsetInBytes int: Offset into the supplied array to begin accessing data.
sizeInBytes int: Number of bytes to write. Offset + size may not exceed buffer length.
Returns
int The number of bytes written.
Throws
IOException If there was an error writing to the device.
IllegalArgumentException If offset or size could lead to array access out of bounds.

write

int write (short[] buffer, 
                int offsetInShorts, 
                int sizeInShorts)

Write raw audio to the audio device. This call blocks until the entire operation is completed.

Parameters
buffer short: Data to write.
offsetInShorts int: Offset into the supplied array to begin accessing data.
sizeInShorts int: Number of shorts to write. Offset + size may not exceed buffer length.
Returns
int The number of shorts written.
Throws
IOException If there was an error writing to the device.
IllegalArgumentException If offset or size could lead to array access out of bounds.
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)