Is it possible to figure out whether a user has a webcam or not using only JavaScript? I don't want to use any plugin for this.
getUserMedia({ video: true }, () => { console. log('has webcam') }, () => { console. log('no webcam') }); to call it with { video: true } and callbacks that's run if a webcam is present and if the webcam isn't present respectively.
You can type "DetectRTC.
By calling the webcam. start() function, the browser will ask user permission to access the camera, once it is allowed, it will start steaming the webcam to the video element.
getUserMedia() method prompts the user for permission to use a media input which produces a MediaStream with tracks containing the requested types of media.
You don't necessarily need to get the permission to know if the user has a webcam, you can check it with enumerateDevices
:
function detectWebcam(callback) { let md = navigator.mediaDevices; if (!md || !md.enumerateDevices) return callback(false); md.enumerateDevices().then(devices => { callback(devices.some(device => 'videoinput' === device.kind)); }) } detectWebcam(function(hasWebcam) { console.log('Webcam: ' + (hasWebcam ? 'yes' : 'no')); })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With