Transcript Introducing HTML and XHTML
Phonegap Bridge – Media
CIS 136 Building Mobile Apps 1
2
Multimedia
Capture API
Multimedia
Camera
Media Access
Media Capture
Accelerometer
Vibration
Splash Screen
3
Capture API
org.apache.cordova.media-capture Provides access to the device's audio, image, and video capture capabilities Collection and use of images, video, or audio from the device's camera or microphone raises important privacy issues Your app's privacy policy should discuss how the app uses such sensors and whether the data recorded is shared with any other parties some app marketplaces may require your app to provide just-in-time notice and obtain permission from the user prior to accessing the camera or microphone 4
Permissions in config file
5
Capture API
assigned to the navigator.device object
therefore has global scope
Four methods:
navigator.device.capture.captureAudio() navigator.device.capture.captureVideo() navigator.device.capture.captureImage() navigator.device.MediaFile.getFormatData()
Each successful callback passes back a mediaFile object relative the type of media being captured
6
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 Duration: maximum number of seconds for audio/video recording 7
Example
function captureAudio() { navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2}); } function captureSuccess(mediaFiles) { var i, len; for (i = 0, len = mediaFiles.length; i < len; i += 1) { path = mediaFiles[i].fullPath; // do something interesting with the file } } } function captureError(error) { var msg = 'An error occurred during capture: ' + error.code; navigator.notification.alert(msg); 8
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 9
Example
function captureImage() { navigator.device.capture.captureImage(captureSuccess, captureError, {limit: 2}); } function captureSuccess(mediaFiles) { var i, len; for (i = 0, len = mediaFiles.length; i < len; i += 1) { } path = mediaFiles[i].fullPath; // do something interesting with the file } } function captureError(error) { var msg = 'An error occurred during capture: ' + error.code; navigator.notification.alert(msg); 10
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 11
Example
function captureVideo() { var options = { limit: 3, duration: 10 }; } navigator.device.capture.captureVideo(captureSuccess, captureError, options); function captureSuccess(mediaFiles) { var i, len; for (i = 0, len = mediaFiles.length; i < len; i += 1) { mediaFiles[i].getFormatData(successCallback,errorCallback); } function successCallback(MediaFileData) { alert(MediaFileData.codec); } function captureError(error) { var msg = 'An error occurred during capture: ' + error.code; navigator.notification.alert(msg); } 12
Success callback function
Invoked upon a successful media capture operation At this point a media file has been captured, and either the user has exited the media capture application, or the capture limit has been reached.
Each MediaFile object describes a 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) 13
CaptureError Object
Encapsulates the error code resulting from a failed media capture operation 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_NOT_SUPPORTED
The requested capture operation is not supported 14
Get Format Data
Retrieves format information about the media capture file.
Example: mediaFile.getFormatData(successCallback,errorCallback); function successCallback(MediaFileData ){ } 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) 15