Previewing camera input in a video element using getUserMedia
works perfectly fine in Chrome, but breaks as soon as I run the same code within a Cordova container. Any ideas what could've caused that?
Added an example using Mozilla gUM polyfill:
(()=>{
var promisifiedOldGUM = function(constraints) {
var getUserMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia
);
if(!getUserMedia) {
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
}
return new Promise(function(resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
}
if(navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if(navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = promisifiedOldGUM;
}
function initCamera() {
const constraints = { audio: false, video: { width: 1280, height: 720 } };
const videoElement = document.createElement('video');
videoElement.style.width = '100px';
videoElement.style.height = '100px';
document.body.appendChild(videoElement);
navigator.mediaDevices
.getUserMedia(constraints)
.then(
stream => {
videoElement.src = window.URL.createObjectURL(stream);
videoElement.onloadedmetadata = () => {
videoElement.play();
};
}
)
.catch(
err => {
console.log('The following error occurred: ' + err.message)
}
);
}
initCamera();
})()
(note that both new and deprecated gUM APIs don't work).
This results in an empty, black <video>
element. No errors are fired.
I assumed initially that this issue was related to AndroidManifest.xml permissions and CSP, but fixing them makes no difference.
CSP config:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' android-webview-video-poster: data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src * blob:">
AndroidManifest.xml (features and permissions)
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />