Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if user has webcam or not using JavaScript only?

Tags:

javascript

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.

like image 959
Jimit Avatar asked Apr 25 '14 09:04

Jimit


People also ask

How do you check if the device has a camera with JavaScript?

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.

Is it possible to check if the user has a camera and microphone and if the permissions have been granted with JavaScript?

You can type "DetectRTC.

Can JavaScript access Webcam?

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.

What is getUserMedia?

getUserMedia() method prompts the user for permission to use a media input which produces a MediaStream with tracks containing the requested types of media.


1 Answers

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')); }) 
like image 192
gtournie Avatar answered Sep 23 '22 00:09

gtournie