Search code examples
javascriptwebmicrophone

How to get hardware information of (in-build) 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.


Solution

  • Newer version: Available in Firefox, MS Edge, and Chrome 45 with experimental flag.

    Using the standard navigator.mediaDevices.enumerateDevices(), you can get a list of available sources. Each source has a kind property, as well as a 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>

    Documentation & Related

    The last demo works in regular Chrome thanks to the adapter.js polyfill.