nio_xr
July 19, 2023, 9:17pm
1
I want to switch the camera with a button click. I know it can be done in the initialization. But say after I initialize with front camera, how do I change the feed to rear camera when a button is clicked. This is what I did, but it does not work
deepAR.stopCamera();
deepAR.startCamera({facingMode: "environment"});
jelena
July 31, 2023, 1:16pm
2
Hi, here’s a code snippet for switching the camera on the Web SDK
function startExternalVideo() {
cleanupVideoObjects();
// create video element
var video = document.createElement('video');
video.muted = true;
video.loop = true;
video.controls = true;
video.setAttribute('playsinline', 'playsinline');
video.style.width = '100%';
video.style.height = '100%';
// put it somewhere in the DOM
var videoContainer = document.createElement('div');
videoContainer.appendChild(video);
videoContainer.style.width = '1px';
videoContainer.style.height = '1px';
videoContainer.style.position = 'absolute';
videoContainer.style.top = '0px';
videoContainer.style.left = '0px';
videoContainer.style['z-index'] = '-1';
document.body.appendChild(videoContainer);
videoObjects.videoContainer = videoContainer;
videoObjects.video = video;
navigator.mediaDevices.getUserMedia({video: true}).then(function(stream) {
try {
video.srcObject = stream;
} catch (error) {
video.src = URL.createObjectURL(stream);
}
setTimeout(function() {
video.play();
}, 50);
}).catch(function(error) {
console.log('error in video play:', error);
});
// tell the DeepAR SDK about our new video element
deepAR.setVideoElement(video, true);
var loaderWrapper = document.getElementById('loader-wrapper');
loaderWrapper.style.display = 'none';
}
function cleanupVideoObjects() {
if (videoObjects.length > 0){
videoObjects.videoContainer.parentNode.removeChild(videoObjects.videoContainer);
videoObjects.videoContainer = null;
if (videoObjects.video.srcObject) {
//getUserMedia starts a stream, all tracks on all streams need to be stoppedbefore calling getUserMedia again
videoObjects.video.srcObject.getTracks().forEach(track => track.stop())
}
videoObjects.video.pause();
videoObjects = {};
}
}