获取本地音视频设备

使用的是navigator.mediaDevices.enumerateDevices()方法
html:


    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>获取本地音视频设备title>
    head>
    <body>
        <h1>获取设备列表h1>
        <table id="deviceList">table>
    body>
html>

js:

<script>
    var deviceList = document.getElementById("deviceList");
    getLocalDevices();
    function getLocalDevices () {
        if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
            console.log("enumerateDevices() not supported.");
            return;
        }
        navigator.mediaDevices.enumerateDevices()
                .then(function(devices) {
                    console.log(devices)
                    var str =
                            "" +
                                "deviceId" +
                                "groupId" +
                                "kind" +
                                "label" +
                            "";
                    for(var i=0;i""+
                                    ""+devices[i].deviceId+""+
                                    ""+devices[i].groupId+""+
                                    ""+devices[i].kind+""+
                                    ""+devices[i].label+""
                                "";
                        deviceList.innerHTML = str;
                    }
                })
                .catch(function(err) {
                    console.log(err.name + ": " + error.message);
                });
    }
script>

你可能感兴趣的:(前端,js,音视频)