VideoRecordEvent.Finalize

public static final class VideoRecordEvent.Finalize
extends VideoRecordEvent

java.lang.Object
   ↳ androidx.camera.video.VideoRecordEvent
     ↳ androidx.camera.video.VideoRecordEvent.Finalize


Indicates the finalization of recording.

The finalize event will be triggered regardless of whether the recording succeeds or fails. Use getError() to obtain the error type and getCause() to get the error cause. If there is no error, 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 ERROR_FILE_SIZE_LIMIT_REACHED. A file may or may not be generated when the recording is finalized with 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

Constants

int ERROR_ENCODING_FAILED

The recording failed while encoding.

int ERROR_FILE_SIZE_LIMIT_REACHED

The recording failed due to file size limitation.

int ERROR_INSUFFICIENT_STORAGE

The recording failed due to insufficient storage space.

int ERROR_INVALID_OUTPUT_OPTIONS

The recording failed due to invalid output options.

int ERROR_NONE

The recording succeeded with no error.

int ERROR_NO_VALID_DATA

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

int ERROR_RECORDER_ERROR

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

int ERROR_SOURCE_INACTIVE

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

int ERROR_UNKNOWN

An unknown error occurred.

Public methods

Throwable getCause()

Gets the error cause.

int getError()

Gets the error type for a video recording.

OutputResults getOutputResults()

Gets the OutputResults.

boolean hasError()

Indicates whether an error occurred.

Inherited methods

Constants

ERROR_ENCODING_FAILED

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.

Constant Value: 6 (0x00000006)

ERROR_FILE_SIZE_LIMIT_REACHED

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.

Constant Value: 2 (0x00000002)

ERROR_INSUFFICIENT_STORAGE

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.

Constant Value: 3 (0x00000003)

ERROR_INVALID_OUTPUT_OPTIONS

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.prepareRecording(android.content.Context, MediaStoreOutputOptions) method. The error will depend on the subclass of OutputOptions used.

No output file will be generated with this error.

Constant Value: 5 (0x00000005)

ERROR_NONE

public static final int ERROR_NONE

The recording succeeded with no error.

Constant Value: 0 (0x00000000)

ERROR_NO_VALID_DATA

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.

Constant Value: 8 (0x00000008)

ERROR_RECORDER_ERROR

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 f