Recorder
class Recorder : VideoOutput
An implementation of VideoOutput for starting video recordings that are saved to a File, ParcelFileDescriptor, or MediaStore.
A recorder can be used to save the video frames sent from the VideoCapture use case in common recording formats such as MPEG4.
Usage example of setting up VideoCapture with a recorder as output:
ProcessCameraProvider cameraProvider = ...; CameraSelector cameraSelector = ...; ... // Create our preview to show on screen Preview preview = new Preview.Builder.build(); // Create the video capture use case with a Recorder as the output VideoCapturevideoCapture = VideoCapture.withOutput(new Recorder.Builder().build()); // Bind use cases to Fragment/Activity lifecycle cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
Once the recorder is attached to a video source as a VideoOutput, e.g. using it to create a VideoCapture by calling withOutput, a new recording can be generated with one of the prepareRecording methods, such as prepareRecording. The PendingRecording class then can be used to adjust per-recording settings and to start the recording. It also requires passing a listener to start to listen for VideoRecordEvents such as VideoRecordEvent.Start, VideoRecordEvent.Pause, VideoRecordEvent.Resume, and VideoRecordEvent.Finalize. This listener will also receive regular recording status updates via the VideoRecordEvent.Status event.
Attaching a single Recorder instance to multiple video sources at the same time may causes unexpected behaviors and is not recommended.
A recorder can also capture and save audio alongside video. The audio must be explicitly enabled with withAudioEnabled before starting the recording.
| See also | |
|---|---|
withOutput |
|
PendingRecording |
Summary
Nested types |
|---|
class Recorder.BuilderBuilder class for |
Constants |
|
|---|---|
const QualitySelector! |
Default quality selector for recordings. |
const Int |
Video capabilities are derived from |
const Int |
Video capabilities are derived from codec capabilities. |
Public functions |
|
|---|---|
Int |
Gets the aspect ratio of this Recorder. |
Executor? |
Returns the executor provided to the builder for this recorder. |
java-static VideoCapabilities? |
getHighSpeedVideoCapabilities(cameraInfo: CameraInfo)Returns the high-speed |
QualitySelector |
Gets the quality selector of this Recorder. |
Int |
Gets the target video encoding bitrate of this Recorder. |
java-static VideoCapabilities |
getVideoCapabilities(cameraInfo: CameraInfo)Returns the |
java-static VideoCapabilities |
getVideoCapabilities(Returns the |
Int |
Gets the video capabilities source of this Recorder. |
Unit |
onSurfaceRequested(request: SurfaceRequest)Called when a new |
PendingRecording |
@RequiresApi(value = 26)Prepares a recording that will be saved to a |
PendingRecording |
prepareRecording(context: Context, fileOutputOptions: FileOutputOptions)Prepares a recording that will be saved to a |
PendingRecording |
prepareRecording(Prepares a recording that will be saved to a |
Constants
DEFAULT_QUALITY_SELECTOR
const val DEFAULT_QUALITY_SELECTOR: QualitySelector!
Default quality selector for recordings.
The default quality selector chooses a video quality suitable for recordings based on device and compatibility constraints. It is equivalent to:
QualitySelector.fromOrderedList(Arrays.asList(Quality.FHD, Quality.HD, Quality.SD), FallbackStrategy.higherQualityOrLowerThan(Quality.FHD));
| See also | |
|---|---|
QualitySelector |
VIDEO_CAPABILITIES_SOURCE_CAMCORDER_PROFILE
const val VIDEO_CAPABILITIES_SOURCE_CAMCORDER_PROFILE = 0: Int
Video capabilities are derived from CamcorderProfile.
The Quality supported by the video capabilities of this source are determined by the device's CamcorderProfile. This means that the quality of recorded videos is guaranteed by the device manufacturer. This is the recommended type for recording high-quality video.
| See also | |
|---|---|
setVideoCapabilitiesSource |
VIDEO_CAPABILITIES_SOURCE_CODEC_CAPABILITIES
const val VIDEO_CAPABILITIES_SOURCE_CODEC_CAPABILITIES = 1: Int
Video capabilities are derived from codec capabilities.
The Quality supported by the video capabilities of this source are determined by the codec capabilities. However, the recorded videos is not guaranteed. For example, there may be frame drops due to thermal throttling or memory pressure. Therefore, it is recommended to use VIDEO_CAPABILITIES_SOURCE_CAMCORDER_PROFILE unless there is a specific reason. A common use case is when the application strives to record UHD video whenever feasible, but the device's CamcorderProfile does not include a UHD quality setting, even though the codec is capable of recording UHD video.
| See also | |
|---|---|
setVideoCapabilitiesSource |
Public functions
getAspectRatio
fun getAspectRatio(): Int
Gets the aspect ratio of this Recorder.
| Returns | |
|---|---|
Int |
the value from |
getExecutor
fun getExecutor(): Executor?
Returns the executor provided to the builder for this recorder.
| Returns | |
|---|---|
Executor? |
the |
getHighSpeedVideoCapabilities
java-static fun getHighSpeedVideoCapabilities(cameraInfo: CameraInfo): VideoCapabilities?
Returns the high-speed VideoCapabilities of Recorder with respect to input camera information.
The returned VideoCapabilities provides methods to query supported dynamic ranges, qualities for high-speed video. For recording high-speed and slow-motion videos, refer to HighSpeedVideoSessionConfig.
| Parameters | |
|---|---|
cameraInfo: CameraInfo |
info about the camera. |
| Returns | |
|---|---|
VideoCapabilities? |
high-speed VideoCapabilities with respect to the input camera info, or null if high-speed video is not supported. |
| See also | |
|---|---|
HighSpeedVideoSessionConfig |
getQualitySelector
fun getQualitySelector(): QualitySelector
Gets the quality selector of this Recorder.
| Returns | |
|---|---|
QualitySelector |
the |
getTargetVideoEncodingBitRate
fun getTargetVideoEncodingBitRate(): Int
Gets the target video encoding bitrate of this Recorder.
| Returns | |
|---|---|
Int |
the value provided to |
getVideoCapabilities
java-static fun getVideoCapabilities(cameraInfo: CameraInfo): VideoCapabilities
Returns the VideoCapabilities of Recorder with respect to input camera information.
VideoCapabilities provides methods to query supported dynamic ranges and qualities. This information can be used for things like checking if HDR is supported for configuring VideoCapture to record HDR video.
The source of the returned VideoCapabilities is VIDEO_CAPABILITIES_SOURCE_CAMCORDER_PROFILE. To get a VideoCapabilities from a different source, use getVideoCapabilities.
| Parameters | |
|---|---|
cameraInfo: CameraInfo |
info about the camera. |
| Returns | |
|---|---|
VideoCapabilities |
VideoCapabilities with respect to the input camera info. |
getVideoCapabilities
java-static fun getVideoCapabilities(
cameraInfo: CameraInfo,
videoCapabilitiesSource: Int
): VideoCapabilities
Returns the VideoCapabilities of Recorder with respect to input camera information and video capabilities source.
VideoCapabilities provides methods to query supported dynamic ranges and qualities. This information can be used for things like checking if HDR is supported for configuring VideoCapture to record HDR video.
The possible video capabilities sources include VIDEO_CAPABILITIES_SOURCE_CAMCORDER_PROFILE and VIDEO_CAPABILITIES_SOURCE_CODEC_CAPABILITIES.
| Parameters | |
|---|---|
cameraInfo: CameraInfo |
info about the camera. |
videoCapabilitiesSource: Int |
the video capabilities source. |
| Returns | |
|---|---|
VideoCapabilities |
VideoCapabilities with respect to the input camera info and video capabilities source. |
getVideoCapabilitiesSource
fun getVideoCapabilitiesSource(): Int
Gets the video capabilities source of this Recorder.
| Returns | |
|---|---|
Int |
the value provided to |
onSurfaceRequested
fun onSurfaceRequested(request: SurfaceRequest): Unit
Called when a new Surface has been requested by a video frame producer.
Users of this class should not call this method directly. It will be called by the video frame producer. Implementors of this class should be aware that this method is called when a video frame producer is ready to receive a surface that it can use to send video frames to the video output. The video frame producer may repeatedly request a surface more than once, but only the latest SurfaceRequest should be considered active. All previous surface requests will complete by sending a androidx.camera.core.SurfaceRequest.Result to the consumer passed to provideSurface.
A request is considered active until it is fulfilled, marked as 'will not complete', or cancelled by the video frame producer. After one of these conditions occurs, a request is considered completed.
Once a request is successfully completed, it is guaranteed that if a new request is made, the Surface used to fulfill the previous request will be detached from the video frame producer and the resultListener provided in provideSurface will be invoked with a androidx.camera.core.SurfaceRequest.Result containing RESULT_SURFACE_USED_SUCCESSFULLY.
| Parameters | |
|---|---|
request: SurfaceRequest |
the request for a surface which contains the requirements of the surface and methods for completing the request. |
prepareRecording
@RequiresApi(value = 26)
fun prepareRecording(
context: Context,
fileDescriptorOutputOptions: FileDescriptorOutputOptions
): PendingRecording
Prepares a recording that will be saved to a ParcelFileDescriptor.
The provided FileDescriptorOutputOptions specifies the ParcelFileDescriptor to use.
Currently, file descriptors as output destinations are not supported on pre-Android O (API 26) devices.
Calling this method multiple times will generate multiple PendingRecordings, each of the recordings can be used to adjust per-recording settings individually. The recording will not begin until start is called. Only a single pending recording can be started per Recorder instance.
| Parameters | |
|---|---|
context: Context |
the context used to enforce runtime permissions, interface with the media scanner service, and attribute access to permission protected data, such as audio. If using this context to audit audio access on API level 31+, a context created with |
fileDescriptorOutputOptions: FileDescriptorOutputOptions |
the options that configures how the output will be handled. |
| Returns | |
|---|---|
PendingRecording |
a |
| Throws | |
|---|---|
java.lang.UnsupportedOperationException |
if this method is called on per-Android O (API 26) devices. |
| See also | |
|---|---|
FileDescriptorOutputOptions |
prepareRecording
fun prepareRecording(context: Context, fileOutputOptions: FileOutputOptions): PendingRecording
Prepares a recording that will be saved to a File.
The provided FileOutputOptions specifies the file to use.
Calling this method multiple times will generate multiple PendingRecordings, each of the recordings can be used to adjust per-recording settings individually. The recording will not begin until start is called. Only a single pending recording can be started per Recorder instance.
| Parameters | |
|---|---|
context: Context |
the context used to enforce runtime permissions, interface with the media scanner service, and attribute access to permission protected data, such as audio. If using this context to audit audio access on API level 31+, a context created with |
fileOutputOptions: FileOutputOptions |
the options that configures how the output will be handled. |
| Returns | |
|---|---|
PendingRecording |
a |
| See also | |
|---|---|
FileOutputOptions |
prepareRecording
fun prepareRecording(
context: Context,
mediaStoreOutputOptions: MediaStoreOutputOptions
): PendingRecording
Prepares a recording that will be saved to a MediaStore.
The provided MediaStoreOutputOptions specifies the options which will be used to save the recording to a MediaStore.
Calling this method multiple times will generate multiple PendingRecordings, each of the recordings can be used to adjust per-recording settings individually. The recording will not begin until start is called. Only a single pending recording can be started per Recorder instance.
| Parameters | |
|---|---|
context: Context |
the context used to enforce runtime permissions, interface with the media scanner service, and attribute access to permission protected data, such as audio. If using this context to audit audio access on API level 31+, a context created with |
mediaStoreOutputOptions: MediaStoreOutputOptions |
the options that configures how the output will be handled. |
| Returns | |
|---|---|
PendingRecording |
a |
| See also | |
|---|---|
MediaStoreOutputOptions |