I have a project in nodeJS which I use on System as well as on Mobile.
I need to perform the below steps - 1. Add a button to turn on/off the flashlight. 2. The button should only be displayed if the feature is supported by the phone and the browser 3. The light should default to off
When I am using the below-mentioned code it is enabling my WebCam flash from my System and it is not working on my mobile.
Flash Light On/Off
//Test browser support
const SUPPORTS_MEDIA_DEVICES = 'mediaDevices' in navigator;
if (SUPPORTS_MEDIA_DEVICES) {
//Get the environment camera (usually the second one)
navigator.mediaDevices.enumerateDevices().then(devices => {
const cameras = devices.filter((device) => device.kind === 'videoinput');
if (cameras.length === 0) {
throw 'No camera found on this device.';
}
const camera = cameras[cameras.length - 1];
// Create stream and get video track
navigator.mediaDevices.getUserMedia({
video: true
}).then(stream => {
const track = stream.getVideoTracks()[0];
track.applyConstraints({
advanced: [{torch: false}]
});
//Create image capture object and get camera capabilities
const imageCapture = new ImageCapture(track)
const photoCapabilities = imageCapture.getPhotoCapabilities().then(() => {
//todo: check if camera has a torch
//let there be light!
const btn = document.querySelector('.switch');
btn.addEventListener('click', function(){
isTorchOn = !isTorchOn;
track.applyConstraints({
advanced: [{torch: isTorchOn}]
});
});
});
});
});
//The light will be on as long the track exists
}
Can anyone suggest a solution?