CIS 136 Building Mobile Apps Media Capture CIS 136 Building Mobile Apps
Multimedia Camera Media Media Capture
Media-Capture API Provides access to the device's audio, image, and video capture capabilities Assigned to the navigator.device.capture object therefore has global scope but not available until deviceready Three methods: navigator.device.capture.captureAudio(success,error,options) navigator.device.capture.captureVideo(success,error,options) navigator.device.capture.captureImage(success,error,options)
SUCCESS CALLBACK FUNCTIONS Invoked upon a successful media capture operation Each successful callback passes back a mediaFiles object which may be a list(array) Each MediaFiles object describes the captured media file Properties name: The name of the file, without path information. (DOMString) fullPath: The full path of the file, including the name. (DOMString) type: The file's mime type (DOMString) lastModifiedDate: The date and time when the file was last modified. (Date) size: The size of the file, in bytes. (Number) Method getFormatData(): Retrieves the format information of the media file.
ERROR CALLBACKS Examine error.code for one of these constant values: Failure scenarios include when the capture application is busy a capture operation is already taking place the user cancels the operation before any media files are captured Examine error.code for one of these constant values: CaptureError.CAPTURE_INTERNAL_ERR The camera or microphone failed to capture image or sound CaptureError.CAPTURE_APPLICATION_BUSY The camera or audio capture application is currently serving another capture request CaptureError.CAPTURE_INVALID_ARGUMENT Invalid use of the API (e.g., the value of limit is less than one) CaptureError.CAPTURE_NO_MEDIA_FILES The user exits the camera or audio capture application before capturing anything CaptureError.CAPTURE_PERMISSION_DENIED The user denied a permission required to perform the given capture request. CaptureError.CAPTURE_NOT_SUPPORTED The requested capture operation is not supported
capture.captureAudio Starts an asynchronous operation to capture audio recordings using the device's default audio recording application allows the user to capture multiple recordings in a single session capture operation ends when either the user exits the audio recording application, or the maximum number of recordings is reached navigator.device.capture.captureAudio(success,fail,options); captureAudio(…) takes three arguments: the name of the function to run when audio is successfully recorded the name of a function to run when an attempt to record audio fails some optional arguments Limit: maximum number of recordings (default:1) Duration: maximum number of seconds for audio/video recording
capture.captureImage Starts an asynchronous operation to capture images using the device's camera application allows the user to capture multiple images in a single session capture operation ends when either the user closes the camera, or the maximum number of recordings is reached navigator.device.capture.captureImage(success,fail,options); captureImage(…) takes three arguments: the name of the function to run when the image is successfully taken the name of a function to run when an attempt to take the image fails some optional arguments Limit: maximum number of image recordings
capture.captureVideo Starts an asynchronous operation to capture video recordings using the device's default video recording application allows the user to capture multiple recordings in a single session capture operation ends when either the user exits the video recording application, or the maximum number of recordings is reached navigator.device.capture.captureVideo(success,fail,options); captureVideo(…) takes three arguments: the name of the function to run when video is successfully recorded the name of a function to run when an attempt to record video fails some optional arguments Limit: maximum number of recordings Duration: maximum number of seconds for audio/video recording
Capture Audio Example function captureAudio() { navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2}); } function captureSuccess(mediaFiles) { var i; var len = mediaFiles.length; for (i = 0; i < len; i ++) { path = mediaFiles[i].fullPath; // do something interesting with the file and check if another file exists } function captureError(error) { var msg = 'An error occurred during capture: ' + error.code; navigator.notification.alert(msg);
Capture Image Example function captureImage() { navigator.device.capture.captureImage(captureSuccess, captureError, {limit: 2}); } function captureSuccess(mediaFiles) { var I; var len = mediaFiles.length; for (i = 0; i < len; i += 1) { imgName = mediaFiles[i].name; // do something interesting with the file } function captureError(error) { var msg = 'An error occurred during capture: ' + error.code; navigator.notification.alert(msg);
Capture Video Example function captureVideo() { var options = { limit: 3, duration: 10 }; navigator.device.capture.captureVideo(captureSuccess, captureError, options); } function captureSuccess(mediaFiles) { var I; var len = mediaFiles.length; for (i = 0; i < len; i += 1) { mediaFiles[i].getFormatData(successCallback,errorCallback); function captureError(error) { var msg = 'An error occurred during capture: ' + error.code; navigator.notification.alert(msg);
Mediafiles object method: Get Format Data Retrieves format information about the media capture file. Example: mediaFiles.getFormatData(successCallback,errorCallback); function successCallback(MediaFileData) { var imgHeight=MediaFileData.height; } MediaFileData properties codecs: The actual format of the audio and video content. (DOMString) bitrate: The average bitrate of the content. The value is zero for images. (Number) height: The height of the image or video in pixels. The value is zero for audio clips. (Number) width: The width of the image or video in pixels. The value is zero for audio clips. (Number) duration: The length of the video or sound clip in seconds. The value is zero for images. (Number)