You'll need to use the following Cordova plugins:
- Cordova Camera - Take photo with camera and get photos/videos from library. Can't be used to record video or audio currently.
- Cordova Media Capture - Record video and audio.
- Cordova File - File API used to upload and delete.
- Cordova Video Editor - Transcode video and create video preview thumbnails.
To add these plugins, run the following commands on your terminal.
cordova plugin add cordova-plugin-camera
cordova plugin add cordova-plugin-media-capture
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-video-editor
This is all you need if you're just using the Cordova Media Upload class, which can be found in js/cordova-media-upload.js. This entire repo is that plus a messaging demo you can try out on a blank Cordova project and uses Framework7. The demo also uses the Cordova Streaming Media plugin to preview videos in the messaging system. Again, this is not necessary if you're just using this class.
cordova plugin add cordova-plugin-streaming-media
Cordova wrapper class to help standardize getting photos, videos and audio from your library or by recording and uploading multiple files at once.
Kind: global class
- CordovaMediaUpload
- new CordovaMediaUpload([optionsObj])
- .capturePhoto([newOptionsObj]) ⇒
promise
- .getPhoto([newOptionsObj]) ⇒
promise
- .captureVideo([newOptionsObj]) ⇒
promise
- .getVideo([newOptionsObj]) ⇒
promise
- .captureAudio() ⇒
promise
- .transcodeVideos(videoUris, [newOptionsObj]) ⇒
promise
- .uploadFiles(serverUrl, paramsObj, fileUris, [videoUriArrObj]) ⇒
promise
- .deleteFiles(fileUris) ⇒
promise
CordovaMediaUpload constructor
Param | Type | Default | Description |
---|---|---|---|
[optionsObj] | object |
{} |
Set options. |
[optionsObj.imageWidth] | int |
Image width | |
[optionsObj.imageHeight] | int |
Image height | |
[optionsObj.imageQuality] | int |
50 |
Image quality as a percent |
[optionsObj.videoWidth] | int |
Video width | |
[optionsObj.videoHeight] | int |
Video height | |
[optionsObj.videoBitRate] | int |
1000000 |
Video bit rate in bits, defaults to 1 megabit (1000000) |
Example
const cmu = new CordovaMediaUpload({
imageWidth: 700,
imageHeight: 700,
videoWidth: 1920,
videoHeight: 1080
});
Take a photo with the camera.
Kind: instance method of CordovaMediaUpload
Fulfil: string
- Local file URI for photo.
Reject: Error
- Rejected promise with message. No message if user cancelled.
Param | Type | Default | Description |
---|---|---|---|
[newOptionsObj] | object |
{} |
New options to set or change. |
Example
cmu.capturePhoto()
.then(fileUri => {})
.catch(error => { if(error) console.log('error'); });
Get existing photo from library.
Kind: instance method of CordovaMediaUpload
Fulfil: string
- Local file URI for photo.
Reject: Error
- Rejected promise with message. No message if user cancelled.
Param | Type | Default | Description |
---|---|---|---|
[newOptionsObj] | object |
{} |
New options to set or change. |
Example
cmu.getPhoto()
.then(fileUri => {})
.catch(error => { if(error) console.log('error'); });
Record video.
Kind: instance method of CordovaMediaUpload
Fulfil: array
[string
imageUri, string
videoUri] - The image and video file URI.
Reject: Error
- Rejected promise with message. No message if user cancelled.
Param | Type | Default | Description |
---|---|---|---|
[newOptionsObj] | object |
{} |
New options to set or change. |
Example
cmu.captureVideo()
.then([imageUri, videoUri] => {})
.catch(error => { if(error) console.log('error'); });
Get existing video from library.
Kind: instance method of CordovaMediaUpload
Fulfil: array
[string
imageUri, string
videoUri] - The image and video file URI.
Reject: Error
- Rejected promise with message. No message if user cancelled.
Param | Type | Default | Description |
---|---|---|---|
[newOptionsObj] | object |
{} |
New options to set or change. |
Example
cmu.getVideo()
.then([imageUri, videoUri] => {})
.catch(error => { if(error) console.log('error'); });
Record audio.
Kind: instance method of CordovaMediaUpload
Fulfil: string
- Local file URI for photo.
Reject: Error
- Rejected promise with message. No message if user cancelled.
Example
cmu.captureAudio()
.then(fileUri => {})
.catch(error => { if(error) console.log('error'); });
Transcoode video.
Kind: instance method of CordovaMediaUpload
Fulfil: string[]
- Array of transcoded video URIs.
Reject: Error
- Rejected promise with message. No message if user cancelled.
Param | Type | Default | Description |
---|---|---|---|
videoUris | Array.<string> | string |
Local video URIs to transcode. | |
[newOptionsObj] | object |
{} |
New options to set or change. |
Example
cmu.transcodeVideos()
.then(videoUris => {})
.catch(error => { if(error) console.log('error'); });
Upload files.
Kind: instance method of CordovaMediaUpload
Fulfil: object[]
- JSON data from server.
Reject: Error
- Fail to convert files to blobs.
Param | Type | Default | Description |
---|---|---|---|
serverUrl | string |
URL to upload files to. | |
paramsObj | object |
parameters to send to server. | |
fileUris | Array.<string> | string |
Local video URIs to upload. | |
[videoUriArrObj] | object |
{} |
The video and thumbnail URI array object |
[videoUriArrObj.thumbs] | Array.<string> |
The video thumbnail URIs | |
[videoUriArrObj.videos] | Array.<string> |
The video URIs |
Example
const serverUrl = 'https://url-to-upload-files';
const paramsObj = {}; //Parameters sent to server
cmu.uploadFiles(serverUrl, paramsObj, fileUris)
.then(data => {})
.catch(error => { if(error) console.log('error'); });
Delete files.
Kind: instance method of CordovaMediaUpload
Fulfil: - The file URI has been deleted.
Reject: Error
- Rejected promise with message.
Param | Type | Description |
---|---|---|
fileUris | Array.<string> | string |
Local video URI to transcode. |
Example
cmu.deleteFiles(fileUris)
.then(() => {})
.catch(error => { if(error) console.log('error'); });