65.9K
CodeProject is changing. Read more.
Home

How to Get List of cam and mic Devices in JavaScript

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (4 votes)

Sep 16, 2014

CPOL
viewsIcon

9306

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.

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