public final class HighSpeedVideoSessionConfig extends SessionConfig
A SessionConfig for high-speed video recording sessions.
This class encapsulates the necessary configurations for a high-speed video recording session, including video capture, optional preview and frame rate. Once configured, this config can be bound to a camera and lifecycle using androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle or androidx.camera.lifecycle.LifecycleCameraProvider.bindToLifecycle.
The supported frame rate range can be queried using CameraInfo.getSupportedFrameRateRanges with a specific HighSpeedVideoSessionConfig. Supported frame rate is at least 120 FPS and is in multiples of 30. Common high-speed frame rates include 120 FPS, 240 FPS, and sometimes higher (like 480 FPS or even 960 FPS on some devices). If the given frame rate is not one of the supported frame rate ranges, an IllegalArgumentException will be thrown when binding to lifecycle.
High-speed recording is subject to specific constraints, aligning with those imposed by Android's camera2 CameraConstrainedHighSpeedCaptureSession API.
Constraints:
-
videoCaptureandpreviewmust not have their target frame rates set explicitly. -
videoCapturemust not have its mirror mode set. -
If
previewis present, its resolution selector, target resolution and target aspect ratio must not be set.previewwill get the same resolution asvideoCapture. Note that the preview stream will not operate at the high frame rate asVideoCapture; instead, it is typically limited to at least 30 FPS by the camera system to ensure a smooth display. -
ViewPortandCameraEffectare not supported.
Recording a high-speed video follows the same process as recording a regular video. This involves creating a Recorder with the desired QualitySelector and other settings. The supported qualities and related settings for high-speed sessions can be queried using Recorder.getHighSpeedVideoCapabilities.
Slow-Motion Video Recording: When the isSlowMotionEnabled flag is set to true, the recorded high-speed video will be processed and saved at a standard frame rate of 30 FPS. This creates the slow-motion effect upon playback. The resulting playback speed will be inversely proportional to the ratio of the high recording frame rate to the standard playback frame rate (30 FPS).
For example:
-
If you record at 120 FPS with
isSlowMotionEnabledset totrue, the video will be saved at 30 FPS, resulting in a 1/4x speed during playback (120 / 30 = 4). -
If you record at 240 FPS with
isSlowMotionEnabledset totrue, the video will be saved at 30 FPS, resulting in a 1/8x speed during playback (240 / 30 = 8).
If isSlowMotionEnabled is false, the video will be saved at the actual recording frame rate specified by the frameRateRange parameter, e.g. 120 FPS, without slow-motion effect.
See the sample code below for recording a slow-motion video:
import androidx.camera.core.CameraSelector import androidx.camera.core.DynamicRange import androidx.camera.core.Preview import androidx.camera.lifecycle.ProcessCameraProvider import androidx.camera.lifecycle.awaitInstance import androidx.camera.video.HighSpeedVideoSessionConfig import androidx.camera.video.Quality import androidx.camera.video.QualitySelector import androidx.camera.video.Recorder import androidx.camera.video.VideoCapture import androidx.core.content.ContextCompat // Get ProcessCameraProvider. val cameraProvider = ProcessCameraProvider.awaitInstance(activity) // Get desired CameraInfo. val cameraInfo = cameraProvider.getCameraInfo(CameraSelector.DEFAULT_BACK_CAMERA) // Get high-speed video capabilities. val videoCapabilities = Recorder.getHighSpeedVideoCapabilities(cameraInfo) if (videoCapabilities == null) { // High-speed video is not supported on this camera device. return } // Get supported Qualities. val supportedQualities = videoCapabilities.getSupportedQualities(DynamicRange.SDR) // App's preferred qualities. val preferredQualities = selectQualities(supportedQualities) // Create UseCases and HighSpeedVideoSessionConfig.Builder val preview = Preview.Builder().build() preview.surfaceProvider = previewView.surfaceProvider val recorder = Recorder.Builder() .setQualitySelector(QualitySelector.fromOrderedList(preferredQualities)) .build() val videoCapture = VideoCapture.withOutput(recorder) val sessionConfigBuilder = HighSpeedVideoSessionConfig.Builder(videoCapture) .setPreview(preview) .setSlowMotionEnabled(true) // Query supported frame rate ranges. val supportedFrameRateRanges = cameraInfo.getSupportedFrameRateRanges(sessionConfigBuilder.build()) // App's preferred frame rate. val preferredFrameRate = selectFrameRate(supportedFrameRateRanges) // Apply preferred frame rate. sessionConfigBuilder.setFrameRateRange(preferredFrameRate) // Bind to lifecycle. cameraProvider.bindToLifecycle( activity, CameraSelector.DEFAULT_BACK_CAMERA, sessionConfigBuilder.build(), ) // Start recording. val recording = recorder.prepareRecording(activity, generateOutputOptions()).start( ContextCompat.getMainExecutor(activity) ) { videoRecordEvent -> // ... }
Summary
Public constructors
Public methods
isSlowMotionEnabled
public final boolean isSlowMotionEnabled()
Whether to apply slow-motion effects to the recorded video.