javascript - How to detect microphone type -
i use webrtc (getusermedia) recording sound , uploading backend server. works except unable determine microphone type (is built-in mic, usb mic, headset mic, sth else?)
does know how can detect type?
you can use navigator.mediadevices.enumeratedevices()
list user's cameras , microphones, , try infer types labels (there's no mic-type field unfortunately).
the following code works in firefox 39 , 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>
in firefox on system, 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 caveats: by spec labels show if device access granted, why snippet asks (try both ways).
furthermore, chrome 45 requires persistent permissions (a bug?) not available in insecure http, may need reload question in https first see labels. if that, don't forget revoke access in url bar afterwards, or chrome persist it, bad idea on stackoverflow!
alternatively, try https://webrtc.github.io/samples/src/content/devices/input-output works in regular chrome adapter.js polyfill, requires grant persistent permission , reload page before see labels (because of how written).
(*) edit: apparently, enumeratedevices got put under experimental flag in chrome 45, need enable as explained here. sorry that. shouldn't long hope.
Comments
Post a Comment