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

前端 未结 6 1597
情书的邮戳
情书的邮戳 2020-12-13 03:09

I would like to find out if the user\'s device has an attached camera and microphone, and if so, has permissions been granted to get the audio and video stream using Javascr

6条回答
  •  北海茫月
    2020-12-13 03:50

    Yes it is quite possible to detect whether a microphone and a camera is available after granting the permission,

    navigator.getUserMedia({ audio: true, video: true},function (stream) {
         if(stream.getVideoTracks().length > 0 && stream.getAudioTracks().length > 0){
             //code for when none of the devices are available                       
         }else{
            // code for when both devices are available
         }
    });
    

提交回复
热议问题