How I can know if I have headphones connected to my computer? HTML5 + JavaScript

浪尽此生 提交于 2021-02-06 09:37:06

问题


How I can know if I have headphones connected to my computer? My goal is to get it using HTML5 + JavaScript.

On the next page shows all audio output devices, but there is no way of knowing if the active device is a headphonet or not. And in Firefox it doesn't work!

https://webrtc.github.io/samples/src/content/devices/input-output/

All I want to know is if I have headphones connected with HTML5 and JavaScript.


回答1:


As of today (2019 march) there is no realiable, cross-browser built-in tool to detect whether a user has headphones plugged in. While you theoretically can get audio output destination device and test it for phrases like headset, earphones etc, in a typical case it rather provides you with driver's name, which usually doesn't change when user plugs his earphones in. The usually part makes this approach useless.




回答2:


There is no way using any existing API, but one way could be to send a sound out while checking if the microphone picks it up.

This would be a real test for echo, you could use some "welcome" sound and some FFT code to "parse" microphone input and see if the sound you are sending is re-entering the microphone. However it would require quite some fine tuning to be reliable, and will still not catch people with low volume on the speakers or catch people with too loud headphones .. even if, in that case, since they are producing echo, catching them is probably the right thing to do.

I can't remember which conferencing software was actually doing this setup step emitting a melody and checking the feedback (maybe it was Skype early versions?), but it's doable in modern browsers.



来源:https://stackoverflow.com/questions/40106777/how-i-can-know-if-i-have-headphones-connected-to-my-computer-html5-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!