WebRTC回顾学习 1 - 浏览器获取到mediaDevices信息

相关API

navigator.medioDevices.enumerateDevices()
	.then(回调)  成功
	.catch(回调) 失败

返回的结果一般为;

deviceInfo  包含 kind: 种类 label: 标签   id 和 groupID
基础执行代码
JS代码
'use strict'
var audioSource  = document.querySelector("select#audioSource");
var audioOutput  = document.querySelector("select#audioOutput");
var videoSource  = document.querySelector("select#videoSource");

if(!navigator.mediaDevices ||
    !navigator.mediaDevices.enumerateDevices){
    console.log('enumerateDevices is not supported!');
}else {
    navigator.mediaDevices.enumerateDevices()
        .then(gotDevices)
        .catch(handleError);
}

function gotDevices(deviceInfos){
    deviceInfos.forEach( function(deviceInfo){
        console.log(deviceInfo.kind + ": label = "
            + deviceInfo.label + ": id = "
            + deviceInfo.deviceId + ": groupId = "
            + deviceInfo.groupId);
        var option = document.createElement('option');
        option.text = deviceInfo.label;
        option.value = deviceInfo.deviceId;
        if(deviceInfo.kind === 'audioinput'){
            audioSource.appendChild(option);
        }else if(deviceInfo.kind === 'audiooutput'){
            audioOutput.appendChild(option);
        }else if(deviceInfo.kind === 'videoinput'){
            videoSource.appendChild(option);
        }
    });

}

function handleError(err){
    console.log(err.name + " : " + err.message);
}


``

```cpp
html
<!DOCTYPE html>
<html lang="en">
<head>

    <title>webRTC</title>

</head>
<body>
<script src="js/client.js"></script>
<div>
    <label>audio input device</label>
    <select id="audioSource"></select>
</div>
<div>
    <label>audio input device</label>
    <select id="audioOutput"></select>
</div>
<div>
    <label>audio input device</label>
    <select id="videoSource"></select>
</div>

</body>
</html>

执行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值