Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

How to Get List of cam and mic Devices in JavaScript

4.60/5 (4 votes)
16 Sep 2014CPOL 9.3K  
How to get list of cam and mic devices in JavaScript

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.

JavaScript
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;
                        }
                    });
                });
            }
        }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)