Cordova Plugin Media Capture
To enhance the functionality of Cordova Plugins, a new Media Capture plugin is used to provide access to several capabilities of the device such as audio, image, and video data. If we talk about the compatibility of the plugin, it is supported in Windows, Android, iOS, Browser platforms. A global navigator.device.capture object is defined by this plugin. If it is in the global scope, in spite, this plugin is not available until after the deviceready event.
Installation:
For installing the media capture plugin, type the below command on command prompt.
Properties:
supportedAudioModes: It mainly refers to the supported audio recording formats for the device.
supportedImageModes: This property refers to the recording image formats and sizes supported for the device.
supportedVideoModes: This property refers to the recording video formats and resolution supported for the device.
Objects:
There are several objects used in the plugin Media Capture which are listed below:
- Capture
- CaptureAudioOptions
- CaptureImageOptions
- CaptureVideoOptions
- CaptureCallback
- CaptureErrorCB
- ConfigurationData
- MediaFile
- MediaFileData
Methods:
Now, it is the time to discuss some methods used in the media plugin:
- capture.captureAudio
- capture.captureImage
- capture.captureVideo
MediaFile.getFormatData
capture.captureAudio:
The main task of this method is to start an audio recorder application and return the information about the captured audio clip files.
For capturing the audio recordings using the device’s default audio recording application, the asynchronous operation is started by using this method. If we talk about the asynchronous operation, multiple audio recordings can be captured by a user in a single session.
The capture operation can be ended in the following cases:
Case1: If a user exits to the audio recording application.
Case2: If maximum number of recordings specified by the CaptureAudioOptions.limit is reached.
The default value of CaptureAudioOptions.limit is 1. Due to this, after recording a single audio clip, the capture operation will be terminated.
The Capturecallback method is executed with an array of MediaFile objects when the capture operation is being finished. The CaptureError.CAPTURE_NO_MEDIA_FILES is encountered if the user terminates the operation before an audio clip is captured.
This method is supported in various platforms such as Windows, iOS, Browser.
Example:
capture.captureImage
The capture.captureImage method is responsible for starting the camera application of the device. It returns the information about the captured image files.
To capture the images using the device’s camera application, the asynchronous operation is started by using this method. By using this operation, a user is capable to capture more than one image in a single session.
The capture operation can be ended in the following cases:
Case1: If a user closes the camera application.
Case2: If maximum number of recordings specified by the CaptureImageOptions.limit is reached.
The default value of CaptureImageOptions.limit is 1. Due to this, after capturing a single image, the capture operation will be terminated.
The CaptureCB method is invoked with an array of MediaFile objects when the capture operation is being finished. The CaptureError.CAPTURE_NO_MEDIA_FILES error code is encountered if the user terminates the operation before an image is captured.
This method is supported in various platforms such as Windows, Android, iOS.
Example:
capture.captureVideo
The capture.captureVideo method is responsible for starting the video recording application of the device. In return, this method returns the information about the captured video clip files.
For capturing the video recordings using the device’s video recording application, the asynchronous operation is started by using this method. If we talk about the asynchronous operation, multiple video recordings can be captured by a user in a single session.
The capture operation can be ended in the following cases:
Case1: If a user closes the video recording application.
Case2: If reached to the maximum number of video recordings specified by the CaptureVideoOptions.limit.
The default value of CaptureVideoOptions.limit is 1. Due to this, after recording a single video clip, the capture operation will be terminated.
The CaptureCB method is invoked with an array of MediaFile objects when the capture operation is being finished. The CaptureError.CAPTURE_NO_MEDIA_FILES error code is encountered if the user terminates the operation before video is captured.
If we talk about the compatibility of this method, it is supported in various platforms such as Android, Windows, iOS.
Example:
Capture.AudioOptions
The main task of the Capture.AudioOptions is to encapsulate an audio capture configuration options.
Properties:
limit: The default value of limit is 1. It refers to the maximum number of audio clips recorded by the user in a single capture operation. The limit value must be greater than or equal to 1.
duration: It refers to the maximum duration of an audio clip. It is denoted in seconds.
Example:
CaptureImageOptions
The main task of the Capture.ImageOptions is to encapsulate an image capture configuration options.
Properties:
limit: The default value of limit is 1. It refers to the maximum number of images captured by the user in a single capture operation. The limit value must be greater than or equal to 1.
Example:
CaptureVideoOptions:
The main task of the Capture.VideoOptions is to encapsulate the video capture configuration options.
Properties:
limit: The default value of limit is 1. It refers to the maximum number of video clips recorded by the user in a single capture operation. The limit value must be greater than or equal to 1.
duration: It refers to the maximum duration of the video clip. It is denoted in seconds.
Example:
CaptureCB
The CaptureCB method is invoked when there is a successful media capture operation.
This method is executed after completing the successful capture operation. The capture operation can be ended in the following cases:
Case1: If a user exists the media capture application.
Case2: If it is reached to the capture limit.To describe the captured media file, the MediaFile object is used.
Example:
CaptureError
For encapsulating the error code when there is a failed media capture operation, CaptureError is used.
Properties:
code: It refers to some of the pre-defined error codes that can be any from the below constants.
Constants:
CaptureError.CAPTURE_INTERNAL_ERR : This error code is shown when a Camera or Microphone is failed to capture an image or sound.
CaptureError.CAPTURE_APPLICATION_BUSY: This error code is used to inform about the camera or audio capture application, in which, it is busy to serve another capture request.
CaptureError.CAPTURE_INVALID_ARGUMENT: If there is an invalid use of API, this error code is thrown.
CaptureError.CAPTURE_ NO_MEDIA_FILES: This error code is thrown if the user closes the camera or audio capture application before capturing anything.
CaptureError.CAPTURE_PERMISSION_DENIED: To perform the capture request, we must need the permission. If a user denied the permission, this error code is being thrown.
CaptureError.CAPTURE_NOT_SUPPORTED: This error code is thrown if the requested capture operation is not being supported.
CaptureErrorCB
During the media capture operation, if any error is occurred, it is informed by the CaptureErrorCB.
When you are trying to launch a media capture operation, an error can occur. Then, this method will be executed if this specific error has occurred. This method is executed in the following cases:
Case1: If the capture application is busy or the capture operation is already taken place.
Case2: If a user cancels an operation before capturing any media files.
This method is executed with a CaptureError object which is containing an error code.
Example:
ConfigurationData
The ConfigurationData encapsulates the set of the media capture parameters in which the device is supported.
The media capture modes are also defined that are supported by the device. MIME type, and capture dimensions for image and video capture are included in the ConfigurationData.
Properties:
type: It represents a media type that involves an ASCII- encoded lowercase string.
height: It includes the height of the image or video. It is represented in pixels.
For defining the height of sound clips, its value is zero.
width: It includes the width of an image or video. It is represented in pixels. For defining the width of sound clips, its value is zero.
Example:
MediaFile.getFormatData
The MediaFile.getFormatData method is used for retrieving the format information of the media capture file.
It performs an asynchronous attempt to get the format information for the media file. The MediaFileDataSuccessCB callback is invoked with the MediaFileData object for the successful operation, whereas, if it is failed, it invokes the MediaFileDataErrorCB callback. This method is supported in various platforms such as Windows, Android, iOS.
Example:
MediaFile
For encapsulating different properties of the media file, this method is used.
Properties:
name: It is used to represent the name of a file.
fullPath: It refers to the full path of the file with its name.
type: It considers the file’s mime type.
lastModifiedDate: It refers to the last modified date and time of the file.
size: It refers to the size of the file which is represented in bytes.
Methods:
MediaFile.getFormatData: For retrieving the format information of media files, this MediaFile.getFormatData method is used.
MediaFileData
To encapsulate the format information about the media file, this method is used.
Properties:
codeecs: This property defines the actual format of audio and video content.
bitrate: It mainly consists of an average bitrate of the specific content. For images, its value is zero.
height: It includes the height of the image or video represented in pixels. Its value is zero for an audio clip.
width: It includes the width of the image or video represented in pixels. For an audio clip, its value is set to zero.
duration: It is represented in seconds which includes the length of the video or sound clip. For images, its value is zero.