Introduction
We can get the list of cam and mic devices in JavaScript using MediaStreamTrack
.
Using the Code
MediaStreamTrack
: The MediaStream
interface represents a stream of media content. A stream consists of severaltracks, like video or audio tracks.
By calling the getSources()
method of the MediaStreamTrack
, we can get all the connected audio and video devices.
function checkDevice(){
if (typeof MediaStreamTrack === 'undefined'){
console.log('This browser does not support MediaStreamTrack.\n\nTry Google Chrome.');
} else {
if(MediaStreamTrack.getSources != undefined){
MediaStreamTrack.getSources(function(sourceInfos){
sourceInfos.forEach(function(oDevice){
switch(oDevice.kind){
case "audio":
console.log("Audio Device Found");
break;
case "video":
console.log("Video Device Found");
break;
}
});
});
}
}
}