【问题标题】:How to detect whether a headphone is inserted or removed in JavaScript?如何在 JavaScript 中检测耳机是否插入或移除?
【发布时间】:2018-06-12 09:11:09
【问题描述】:

JavaScript中有没有事件,我可以在浏览器中监听,通过它我们可以知道耳机是插入还是移除?

我假设如果我们能够在 JavaScript 中迭代音频输出设备,是否有可能,我们可以扣除音频输出设备数量的变化?

【问题讨论】:

标签: javascript browser headphones


【解决方案1】:

您可以使用以下内容:https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices

示例:

navigator.mediaDevices.addEventListener('devicechange', () => {
  // Do whatever you need to with the devices
  // Maybe use enumerateDevices() to see what connected
});

您可以通过以下方式检查已连接的设备或拨打devicechange

navigator.mediaDevices.enumerateDevices()
  .then(devices => {
    // Check the connected devices
    console.log(devices);  
  });

【讨论】:

    【解决方案2】:

    我正在使用 phone-gap 插件

    HTML:

    <button class="button button-stable" ng-click="checkHeadphone()">
    

    JS:

    $scope.checkHeadphone = function () {
            window.plugins.headsetdetection.detect(function (detected) {
                alert("Headphone " + detected)
            })
        }
    

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2011-04-04
      • 1970-01-01
      • 2011-09-09
      • 2016-02-25
      • 2013-04-17
      • 2015-05-09
      • 2013-04-29
      • 2012-05-27
      相关资源
      最近更新 更多