public class

ProjectedCarAudioRecord

extends CarAudioRecord

 java.lang.Object

androidx.car.app.media.CarAudioRecord

↳androidx.car.app.media.ProjectedCarAudioRecord

Gradle dependencies

compile group: 'androidx.car.app', name: 'app-projected', version: '1.7.0-beta01'

  • groupId: androidx.car.app
  • artifactId: app-projected
  • version: 1.7.0-beta01

Artifact androidx.car.app:app-projected:1.7.0-beta01 it located at Google repository (https://maven.google.com/)

Overview

A CarAudioRecord for projection.

Summary

Fields
from CarAudioRecordAUDIO_CONTENT_BUFFER_SIZE, AUDIO_CONTENT_MIME, AUDIO_CONTENT_SAMPLING_RATE
Constructors
publicProjectedCarAudioRecord(CarContext carContext)

Methods
protected abstract intreadInternal(byte[] audioData[], int offsetInBytes, int sizeInBytes)

Performs internal platform specific read behavior.

protected abstract voidstartRecordingInternal(OpenMicrophoneResponse openMicrophoneResponse)

Performs internal platform specific start recording behavior.

protected abstract voidstopRecordingInternal()

Performs internal platform specific stop recording behavior.

from CarAudioRecordcreate, read, startRecording, stopRecording
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ProjectedCarAudioRecord(CarContext carContext)

Methods

protected abstract void startRecordingInternal(OpenMicrophoneResponse openMicrophoneResponse)

Performs internal platform specific start recording behavior.

Parameters:

openMicrophoneResponse: the response from the host for opening the microphone

protected abstract void stopRecordingInternal()

Performs internal platform specific stop recording behavior.

protected abstract int readInternal(byte[] audioData[], int offsetInBytes, int sizeInBytes)

Performs internal platform specific read behavior.

Parameters:

audioData: the array to which the recorded audio data is written
offsetInBytes: index in audioData from which the data is written expressed in bytes
sizeInBytes: the number of requested bytes

Returns:

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

Source

/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.car.app.media;

import static android.Manifest.permission.RECORD_AUDIO;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static androidx.car.app.utils.LogTags.TAG;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
import androidx.annotation.RestrictTo;
import androidx.car.app.CarContext;
import androidx.car.app.annotations.CarProtocol;
import androidx.car.app.annotations.KeepFields;

import java.io.IOException;
import java.io.InputStream;

/**
 * A {@link CarAudioRecord} for projection.
 *
 */
@RestrictTo(LIBRARY_GROUP)
@KeepFields
@CarProtocol
public class ProjectedCarAudioRecord extends CarAudioRecord {
    @Nullable
    private InputStream mInputStream;

    @RequiresPermission(RECORD_AUDIO)
    public ProjectedCarAudioRecord(
            @NonNull CarContext carContext) {
        super(carContext);
    }

    @Override
    protected void startRecordingInternal(@NonNull OpenMicrophoneResponse openMicrophoneResponse) {
        mInputStream = openMicrophoneResponse.getCarMicrophoneInputStream();
    }

    @Override
    protected void stopRecordingInternal() {
        try {
            if (mInputStream != null) {
                mInputStream.close();
                mInputStream = null;
            }
        } catch (IOException e) {
            Log.e(TAG, "Exception closing microphone pipe", e);
        }
    }

    @Override
    protected int readInternal(@NonNull byte[] audioData, int offsetInBytes, int sizeInBytes) {
        InputStream inputStream = mInputStream;

        if (inputStream != null) {
            try {
                return inputStream.read(audioData, offsetInBytes, sizeInBytes);
            } catch (IOException e) {
                // stream is closed
                stopRecording();
            }
        }
        return -1;
    }
}