public final class

VideoRecordEvent.Finalize

extends VideoRecordEvent

 java.lang.Object

androidx.camera.video.VideoRecordEvent

↳androidx.camera.video.VideoRecordEvent.Finalize

Overview

Indicates the finalization of recording.

The finalize event will be triggered regardless of whether the recording succeeds or fails. Use VideoRecordEvent.Finalize.getError() to obtain the error type and VideoRecordEvent.Finalize.getCause() to get the error cause. If there is no error, VideoRecordEvent.Finalize.ERROR_NONE will be returned. Other error types indicate the recording is failed or stopped due to a certain reason. Please note that receiving a finalize event with error does not necessarily mean that the video file has not been generated. In some cases, the file can still be successfully generated depending on the error type. For example, a file will still be generated when the recording is finalized with VideoRecordEvent.Finalize.ERROR_FILE_SIZE_LIMIT_REACHED. A file may or may not be generated when the recording is finalized with VideoRecordEvent.Finalize.ERROR_INSUFFICIENT_STORAGE. Example to detect if an output file is generated:

 if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
     VideoRecordEvent.Finalize finalizeEvent =
             (VideoRecordEvent.Finalize) videoRecordEvent;
     OutputOptions options = finalizeEvent.getOutputOptions();
     switch (finalizeEvent.getError()) {
     case ERROR_INSUFFICIENT_STORAGE:
         if (options instanceof FileOutputOptions) {
             if (((FileOutputOptions) options).getFile().exists()) {
                 // file exists
             }
         } else if (options instanceof MediaStoreOutputOptions) {
             Uri uri = finalizeEvent.getOutputResults().getOutputUri();
             if (uri != Uri.EMPTY) {
                 // file exists
             }
         } else if (options instanceof FileDescriptorOutputOptions) {
             // User has to check the referenced target of the file descriptor.
         }
         break;
     }
 }
 

For certain types of errors, the output file will not be constructed correctly, and it will be the user's responsibility to deal with the incomplete file, such as deleting it. Example to delete the file:

 if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
     VideoRecordEvent.Finalize finalizeEvent =
             (VideoRecordEvent.Finalize) videoRecordEvent;
     OutputOptions options = finalizeEvent.getOutputOptions();
     switch (finalizeEvent.getError()) {
     case ERROR_UNKNOWN:
     case ERROR_RECORDER_ERROR:
     case ERROR_ENCODING_FAILED:
     case ERROR_NO_VALID_DATA:
         if (options instanceof FileOutputOptions) {
             ((FileOutputOptions) options).getFile().delete();
         } else if (options instanceof MediaStoreOutputOptions) {
             Uri uri = finalizeEvent.getOutputResults().getOutputUri();
             if (uri != Uri.EMPTY) {
                 context.getContentResolver().delete(uri, null, null);
             }
         } else if (options instanceof FileDescriptorOutputOptions) {
             // User has to clean up the referenced target of the file descriptor.
         }
         break;
     }
 }
 

If there's no error that prevents the file to be generated, the file can be accessed safely after receiving the finalize event.

Summary

Fields
public static final intERROR_ENCODING_FAILED

The recording failed while encoding.

public static final intERROR_FILE_SIZE_LIMIT_REACHED

The recording failed due to file size limitation.

public static final intERROR_INSUFFICIENT_STORAGE

The recording failed due to insufficient storage space.

public static final intERROR_INVALID_OUTPUT_OPTIONS

The recording failed due to invalid output options.

public static final intERROR_NO_VALID_DATA

The recording failed because no valid data was produced to be recorded.

public static final intERROR_NONE

The recording succeeded with no error.

public static final intERROR_RECORDER_ERROR

The recording failed because the Recorder is in an unrecoverable error state.

public static final intERROR_SOURCE_INACTIVE

The recording failed because the source becomes inactive and stops sending frames.

public static final intERROR_UNKNOWN

An unknown error occurred.

Methods
public java.lang.ThrowablegetCause()

Gets the error cause.

public intgetError()

Gets the error type for a video recording.

public OutputResultsgetOutputResults()

Gets the OutputResults.

public booleanhasError()

Indicates whether an error occurred.

from VideoRecordEventgetOutputOptions, getRecordingStats
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int ERROR_NONE

The recording succeeded with no error.

public static final int ERROR_UNKNOWN

An unknown error occurred.

The output file may or moy not be generated. Since the error is not determined, application should clean up the output file, such as deleting it.

public static final int ERROR_FILE_SIZE_LIMIT_REACHED

The recording failed due to file size limitation.

The file size limitation will refer to OutputOptions.getFileSizeLimit(). The output file will still be generated with this error.

public static final int ERROR_INSUFFICIENT_STORAGE

The recording failed due to insufficient storage space.

There are two possible cases that will cause this error.

  • The storage is already full before the recording starts, so no output file will be generated.
  • The storage becomes full during recording, so the output file will be generated.

public static final int ERROR_SOURCE_INACTIVE

The recording failed because the source becomes inactive and stops sending frames.

One case is that if camera is closed due to lifecycle stopped, the active recording will be finalized with this error, and the output will be generated, containing the frames produced before camera closing. Attempting to start a new recording will be finalized immediately if the source remains inactive and no output will be generated.

public static final int ERROR_INVALID_OUTPUT_OPTIONS

The recording failed due to invalid output options.

This error is generated when invalid output options have been used while preparing a recording, such as with the Recorder method. The error will depend on the subclass of OutputOptions used.

No output file will be generated with this error.

public static final int ERROR_ENCODING_FAILED

The recording failed while encoding.

This error may be generated when the video or audio codec encounters an error during encoding. When this happens and the output file is generated, the output file is not properly constructed. The application will need to clean up the output file, such as deleting the file.

public static final int ERROR_RECORDER_ERROR

The recording failed because the Recorder is in an unrecoverable error state.

When this happens and the output file is generated, the output file is not properly constructed. The application will need to clean up the output file, such as deleting the file. Such an error will usually require creating a new Recorder object to start a new recording.

public static final int ERROR_NO_VALID_DATA

The recording failed because no valid data was produced to be recorded.

This error is generated when the essential data for a recording to be played correctly is missing, for example, a recording must contain at least one key frame. The application will need to clean up the output file, such as deleting the file.

Methods

public OutputResults getOutputResults()

Gets the OutputResults.

public boolean hasError()

Indicates whether an error occurred.

Returns true if VideoRecordEvent.Finalize.getError() returns VideoRecordEvent.Finalize.ERROR_NONE, otherwise false.

public int getError()

Gets the error type for a video recording.

Possible values are VideoRecordEvent.Finalize.ERROR_NONE, VideoRecordEvent.Finalize.ERROR_UNKNOWN, VideoRecordEvent.Finalize.ERROR_FILE_SIZE_LIMIT_REACHED, VideoRecordEvent.Finalize.ERROR_INSUFFICIENT_STORAGE, VideoRecordEvent.Finalize.ERROR_INVALID_OUTPUT_OPTIONS, VideoRecordEvent.Finalize.ERROR_ENCODING_FAILED, VideoRecordEvent.Finalize.ERROR_RECORDER_ERROR, VideoRecordEvent.Finalize.ERROR_NO_VALID_DATA and VideoRecordEvent.Finalize.ERROR_SOURCE_INACTIVE.

public java.lang.Throwable getCause()

Gets the error cause.

Returns null if VideoRecordEvent.Finalize.hasError() returns false.