CarAudioRecord


@RequiresCarApi(value = 5)
public abstract class CarAudioRecord


The CarAudioRecord class manages access car microphone audio.

This is done by reading the data via calls to read.

The size of the internal buffer for audio data is defined by AUDIO_CONTENT_BUFFER_SIZE. Data should be read from this in chunks of sizes smaller or equal to this value.

The sample rate is defined by AUDIO_CONTENT_SAMPLING_RATE.

The content mime tipe is defined by AUDIO_CONTENT_MIME.

Whenever the user dismisses the microphone on the car screen, the next call to read will return -1. When the read call returns -1, it means the user has dismissed the microphone and the data can be ignored

API Usage Example
CarAudioRecord car = CarAudioRecord.create(carContext);
car.startRecording();

byte[] data = new byte[CarAudioRecord.AUDIO_CONTENT_BUFFER_SIZE];
while(car.read(data, 0, CarAudioRecord.AUDIO_CONTENT_BUFFER_SIZE) >= 0) {
    // Use data array
    // Potentially calling car.stopRecording() if your processing finds end of speech
}
car.stopRecording();
Audio Focus When recording the car microphone, you should first acquire audio focus, to ensure that any ongoing media is stopped. If you lose audio focus, you should stop recording. Here is an example of how to acquire audio focus:
CarAudioRecord record = CarAudioRecord.create(carContext);
// Take audio focus so that user's media is not recorded
AudioAttributes audioAttributes =
        new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .setUsage(USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
                .build();

AudioFocusRequest audioFocusRequest =
        new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
                .setAudioAttributes(audioAttributes)
                .setOnAudioFocusChangeListener(state -> {
                    if (state == AudioManager.AUDIOFOCUS_LOSS) {
                        // Stop recording if audio focus is lost
                        record.stopRecording();
                    }
                })
                .build();

if (mCarContext.getSystemService(AudioManager.class).requestAudioFocus(audioFocusRequest)
        != AUDIOFOCUS_REQUEST_GRANTED) {
    return;
}

record.startRecording();

Summary

Constants

static final int

The default buffer size of audio reads from the microphone.

static final String
AUDIO_CONTENT_MIME = "audio/l16"

The mime type for raw audio.

static final int

The sampling rate of the audio.

Public methods

static @NonNull CarAudioRecord
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
create(@NonNull CarContext carContext)

Creates a CarAudioRecord.

int
read(@NonNull byte[] audioData, int offsetInBytes, int sizeInBytes)

Reads audio data from the car microphone for recording into a byte array.

void

Starts recording the car microphone.

void

Stops recording the car microphone.

Constants

AUDIO_CONTENT_BUFFER_SIZE

Added in 1.3.0
public static final int AUDIO_CONTENT_BUFFER_SIZE = 512

The default buffer size of audio reads from the microphone.

AUDIO_CONTENT_MIME

Added in 1.3.0
public static final String AUDIO_CONTENT_MIME = "audio/l16"

The mime type for raw audio. The car API samples audio at 16khz.

AUDIO_CONTENT_SAMPLING_RATE

Added in 1.3.0
public static final int AUDIO_CONTENT_SAMPLING_RATE = 16000

The sampling rate of the audio.

Public methods

create

Added in 1.3.0
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
public static @NonNull CarAudioRecord create(@NonNull CarContext carContext)

Creates a CarAudioRecord.

Throws
java.lang.NullPointerException

if carContext is null

read

Added in 1.3.0
public int read(@NonNull byte[] audioData, int offsetInBytes, int sizeInBytes)

Reads audio data from the car microphone for recording into a byte array.

Parameters
@NonNull byte[] audioData

the array to which the recorded audio data is written

int offsetInBytes

index in audioData from which the data is written expressed in bytes

int sizeInBytes

the number of requested bytes

Returns
int

the number of bytes that were read, or -1 if there isn't any more microphone data to read. The number of bytes will be a multiple of the frame size in bytes not to exceed sizeInBytes

Throws
java.lang.IllegalStateException

if called before calling startRecording or after calling stopRecording

startRecording

Added in 1.3.0
public void startRecording()

Starts recording the car microphone.

Read the microphone input via calling read

When finished processing microphone input, call stopRecording

stopRecording

Added in 1.3.0
public void stopRecording()

Stops recording the car microphone.