How to set up CameraX (Android) to stop recording after 5 seconds?

Here's the current code that I use. I don't know where to call mCameraView.stopRecording() if I want to stop recording the video automatically after 5 seconds. The current approach is through the setOnClickListener of the button click.

build.gradle

def camerax_version = "1.0.0-beta08"
    
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha15"
implementation "androidx.camera:camera-extensions:1.0.0-alpha15"

MainActivity.java

CameraView mCameraView;

mCameraView.setCaptureMode(CameraView.CaptureMode.VIDEO);
mCameraView.startRecording(file, executor, new VideoCapture.OnVideoSavedCallback() {

    @Override
    public void onVideoSaved(@NonNull OutputFileResults outputFileResults) {
            // save video file
            }
    
    @Override
    public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
           
            mCameraView.stopRecording();
}

Upvotes: 2

Views: 3039

Answers (2)

V. Boréal
V. Boréal

Reputation: 31

Version 1.3.0-alpha01 introduced OutputOptions.setDurationLimit, renamed to setDurationLimitMillis on alpha02.

Here is an example with MediaStoreOutputOptions :

MediaStoreOutputOptions mediaStoreOutputOptions = new MediaStoreOutputOptions
        .Builder(getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
        .setContentValues(contentValues)
        .setDurationLimitMillis(5000)
        .build();

Upvotes: 3

MobileMon
MobileMon

Reputation: 8651

Run your code after a delay

 final Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
         mCameraView.stopRecording();
      }
    }, 5000);

Upvotes: 3

Related Questions