如何检测JavaScript中是否插入或删除了耳机?

问题描述 投票:3回答:2

JavaScript中是否有任何事件,我可以在浏览器中收听,通过它我们可以知道是否插入或删除了耳机?

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

javascript browser headphones
2个回答
3
投票

您可以使用以下内容: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);  
  });

0
投票

我正在使用手机插件插件

HTML:

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

JS:

$scope.checkHeadphone = function () {
        window.plugins.headsetdetection.detect(function (detected) {
            alert("Headphone " + detected)
        })
    }
© www.soinside.com 2019 - 2024. All rights reserved.