如何检测麦克风类型 [英] How to detect microphone type

查看:45
本文介绍了如何检测麦克风类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 webRTC (getUserMedia) 录制声音并将其上传到后端服务器.一切正常,除了我无法确定麦克风类型(它是内置麦克风、USB 麦克风、耳机麦克风还是其他?)

I use webRTC (getUserMedia) for recording sound and uploading it to backend server. All works well except i am unable to determine the microphone type (is it a built-in mic, usb mic, headset mic, sth else?)

有人知道我如何检测类型吗?

Does anybody know how can i detect the type?

推荐答案

您可以使用 navigator.mediaDevices.enumerateDevices() 列出用户的摄像头和麦克风,并尝试从他们的标签推断类型(遗憾的是没有 mic-type 字段).

You can use navigator.mediaDevices.enumerateDevices() to list the user's cameras and microphones, and try to infer types from their labels (there's no mic-type field unfortunately).

以下代码适用于 Firefox 39 和 Chrome 45 *:

The following code works in Firefox 39 and Chrome 45 *:

var stream;
navigator.mediaDevices.getUserMedia({ audio:true })
.then(s => (stream = s), e => console.log(e.message))
.then(() => navigator.mediaDevices.enumerateDevices())
.then(devices => {
  stream && stream.stop();
  console.log(devices.length + " devices.");
  devices.forEach(d => console.log(d.kind + ": " + d.label));
})
.catch(e => console.log(e));

var console = { log: msg => div.innerHTML += msg + "<br>" };

<div id="div"></div>

在我系统上的 Firefox 中,这会产生:

In Firefox on my system, this produces:

5 devices.
videoinput: Logitech Camera
videoinput: FaceTime HD Camera (Built-in)
audioinput: default (Logitech Camera)
audioinput: Built-in Microphone
audioinput: Logitech Camera

现在,有一些警告:根据规范标签仅显示是否授予设备访问权限,这就是代码段要求它的原因(两种方式都尝试).

Now, there are some caveats: By spec the labels only show if device access is granted, which is why the snippet asks for it (try it both ways).

此外,Chrome 45 需要持久权限(一个错误?),这在不安全的 HTTP 中不可用,因此您可能需要 首先在 HTTPS 中重新加载此问题 以查看标签.如果这样做,请不要忘记事后在 URL 栏中撤消访问权限,否则 Chrome 会保留它,这对 stackoverflow 来说可能是个坏主意!

Furthermore, Chrome 45 requires persistent permissions (a bug?) which is not available in insecure HTTP, so you may need to reload this question in HTTPS first to see labels. If you do that, don't forget to revoke access in the URL bar afterwards, or Chrome will persist it, which is probably a bad idea on stackoverflow!

或者,尝试 https://webrtc.github.io/samples/src/content/devices/input-output 由于 adapter.js polyfill,但需要您授予持久权限并在看到标签之前重新加载页面(因为它是如何编写的).

Alternatively, try https://webrtc.github.io/samples/src/content/devices/input-output which works in regular Chrome thanks to the adapter.js polyfill, but requires you to grant persistent permission and reload the page before you see labels (because of how it was written).

(*) 显然, enumerateDevices 刚刚在 Chrome 45 中被放回一个实验标志下,所以你需要启用它如此处所述.对于那个很抱歉.我希望应该不会太久.

(*) Apparently, enumerateDevices just got put back under an experimental flag in Chrome 45, so you need to enable it as explained here. Sorry about that. Shouldn't be long I hope.

这篇关于如何检测麦克风类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆