如何使用JavaScript检测与麦克风连接的系统 [英] How to detect that system connected with microphone using JavaScript

查看:121
本文介绍了如何使用JavaScript检测与麦克风连接的系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 getUserMedia()进行录音,但它运行正常但有问题。

I'm using getUserMedia() for audio recording and it works correctly but have an issue with it.

我想在开始录制之前显示一条消息,说明任何麦克风是否与系统连接。

I want to display a message before starting recording that any microphone is connected with system or not.

为此,我使用了以下代码并将其运行到chrome但是它工作不正常。

For this I have used following code and run this into chrome but it was not working correctly.

if(navigator.getUserMedia || navigator.webkitGetUserMedia)
{
   alert("Microphone is connected with your system");
} else {
   alert("Microphone is not connected with your system");
}

当麦克风未连接时,也会在代码上方显示消息麦克风已连接你的系统。

when microphone is not connected then also above code giving message "Microphone is connected with your system".

所以请建议我在任何浏览器中使用JavaScript检测麦克风的更好方法。

so please suggest me a better way to detect microphone using JavaScript in any browser.

推荐答案

测试这些功能是否存在并未检测到硬件麦克风的存在。它只检测浏览器是否有API来执行此操作。

Testing for the existence of these functions does not detect the existence of hardware microphone. It only detects if browser has the API to do so.

通过测试的浏览器不需要将物理麦克风插入麦克风插孔。它只是一个较新的浏览器。未通过测试的浏览器可能有麦克风,但是旧浏览器不包含API。

The browsers that pass your test need not have a physical microphone plugged into microphone jack. It is simply a newer browser. The browsers that fail the test may have a microphone, but are old browsers that do not contain the API.

此外,至少getUserMedia函数是异步的,因此任何代码依赖于使用音频或视频必须放在回调函数中,而不是主脚本。

Also, at least the getUserMedia function is asynchronous, so any code that depends on using the audio or video must be put in a callback function, not the main script.

参见 https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getUserMedia for如何为音频/视频输入编写跨浏览器代码的示例。

See https://developer.mozilla.org/en-US/docs/Web/API/Navigator.getUserMedia for an example of how to write cross-browser code for audio/video input.

这篇关于如何使用JavaScript检测与麦克风连接的系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆