CarAudioRecord


@RequiresCarApi(value = 5)
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

const Int

The default buffer size of audio reads from the microphone.

const String!
AUDIO_CONTENT_MIME = "audio/l16"

The mime type for raw audio.

const Int

The sampling rate of the audio.

Public functions

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

Creates a CarAudioRecord.

Int
read(audioData: ByteArray, offsetInBytes: Int, sizeInBytes: Int)

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

Unit

Starts recording the car microphone.

Unit

Stops recording the car microphone.

Constants

AUDIO_CONTENT_BUFFER_SIZE

Added in 1.3.0
const val AUDIO_CONTENT_BUFFER_SIZE = 512: Int

The default buffer size of audio reads from the microphone.

AUDIO_CONTENT_MIME

Added in 1.3.0
const val AUDIO_CONTENT_MIME = "audio/l16": String!

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

AUDIO_CONTENT_SAMPLING_RATE

Added in 1.3.0
const val AUDIO_CONTENT_SAMPLING_RATE = 16000: Int

The sampling rate of the audio.

Public functions

create

Added in 1.3.0
@RequiresPermission(value = "android.permission.RECORD_AUDIO")
java-static fun create(carContext: CarContext): CarAudioRecord

Creates a CarAudioRecord.

Throws
java.lang.NullPointerException

if carContext is null

read

Added in 1.3.0
fun read(audioData: ByteArray, offsetInBytes: Int, sizeInBytes: Int): Int

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

Parameters
audioData: ByteArray

the array to which the recorded audio data is written

offsetInBytes: Int

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

sizeInBytes: Int

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
fun startRecording(): Unit

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
fun stopRecording(): Unit

Stops recording the car microphone.