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

时间:2015-07-31 20:18:28

标签: javascript web microphone

Is it possible to read the hardware information (at least the name) of the (in-build) microphone while a user is recording an audio file on my website?

Is that possible with JavaScript or is there another way to solve this problem? I searched the web but could only find scripts for recording with JavaScript.

2 个答案:

答案 0 :(得分:8)

较新版本:适用于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>

文档&amp;相关

由于adapter.js polyfill。

,最后一个演示版适用于常规Chrome

答案 1 :(得分:1)

过时的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
    }
});

文档&amp;相关