如何获取(内置)麦克风的硬件信息

How to get hardware information of (in-build) microphone?

本文关键字:硬件 信息 麦克风 内置 获取 何获取      更新时间:2023-09-26

是否可以读取(内置)麦克风的硬件信息(至少名称),而用户正在我的网站上录制音频文件?

是可能的JavaScript或有其他方法来解决这个问题?我搜索了网页,但只能找到用JavaScript记录的脚本。

新版本:适用于Firefox, MS Edge和Chrome 45,带有实验标志。

使用标准的navigator.mediaDevices.enumerateDevices(),您可以获得可用资源的列表。每个源都有一个kind属性,以及一个label属性。

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>

文档,相关的

  • navigator.mediaDevices on MDN - https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices
  • "媒体捕获和流"规范- http://w3c.github.io/mediacapture-main/getusermedia.html#mediadevices
  • Sam Dutton选择输入源的演示- https://webrtc.github.io/samples/src/content/devices/input-output/

最后一个演示可以在普通Chrome中工作,这要归功于adapter.js polyfill。

过时的API

这个答案使用了一个非标准的API,浏览器支持有限。它可以在当前的Chrome中工作,但不会在其他浏览器的未来版本中采用,并且可能会在Chrome中消失。有关具有更广泛支持的解决方案,请参见:https://stackoverflow.com/a/31758598/610573


使用MediaStreamTrack.getSources(),您可以获得可用源代码的列表。每个源都有一个kind属性,以及一个label

MediaStreamTrack.getSources(function(sourceInfos) {
    for (var i = 0; i != sourceInfos.length; ++i) {
        var thisSource = sourceInfos[i];
        console.log('stream type: '+thisSource.kind+', label: '+thisSource.label); 
        // example: stream type: audio, label: internal microphone
    }
});

文档,相关的

  • MediaStreamTrack on MDN - https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack
  • "捕捉音频&HTML5视频"在HTML5Rocks.com - http://www.html5rocks.com/en/tutorials/getusermedia/intro/
  • Sam Dutton选择输入源的演示- https://simpl.info/getusermedia/sources/