I am trying without success to use the readAsDataURL function of the Cordova File plugin to get a base64 version of a video file. My code looks like this:
recordVideo()
{
return new Promise(resolve =>
{
let options: CaptureVideoOptions = { limit: 1, duration: 2 };
MediaCapture.captureVideo(options)
.then(
(data: MediaFile[]) => {
console.log('Media: recordVideo: cordova.file.dataDirectory = ' + cordova.file.dataDirectory + ', path = ' + data[0].fullPath.substring(1));
// Turn the video file into base64
let base64File = File.readAsDataURL(cordova.file.dataDirectory, data[0].fullPath.substring(1));
console.log('Media: recordVideo: got video with data = ' + JSON.stringify(data));
console.log('Media: recordVideo: base64File = ' + JSON.stringify(base64File));
resolve(data);
},
(err: CaptureError) => console.error('ERROR - Media: recordVideo: captureVideo error = ' + err)
);
});
}
The output from the first console.log
shows the values of the parameters passed to the readAsDataURL
:
Media: recordVideo: cordova.file.dataDirectory = file:///var/mobile/Containers/Data/Application/764345DC-A77D-43C2-9DF7-CDBE6A0DC372/Library/NoCloud/, path = private/var/mobile/Containers/Data/Application/764345DC-A77D-43C2-9DF7-CDBE6A0DC372/tmp/50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV
The second console.log
shows the data returned by the MediaCapture plugin:
Media: recordVideo: got video with data = [{"name":"50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV","localURL":"cdvfile://localhost/temporary/50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV","type":"video/quicktime","lastModified":null,"lastModifiedDate":1485446813000,"size":195589,"start":0,"end":0,"fullPath":"/private/var/mobile/Containers/Data/Application/764345DC-A77D-43C2-9DF7-CDBE6A0DC372/tmp/50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV"}]
The last console.log
shows the value returned by the readAsDataURL
:
Media: recordVideo: base64File = {"__zone_symbol__state":null,"__zone_symbol__value":[]}
There is next to no documentation on using this (that I can find).
Function readAsDataURL gets path and filename as parameters and returns a promise. The usage is
File.readAsDataURL("path_to_the_FileName", "Filename").then(result => {
this.base64File = result;
});
As per the console log, the filename and full path to the filename are obtained from data (promise returned from MediaCapture.captureVideo).
So you can use it as below
var path = "file://"+data[0].fullPath.substring(7,data[0].fullPath.lastIndexOf("/"));
File.readAsDataURL(path, data[0].name).then(result => {
this.base64File = result;
}