检查Javascript中的麦克风音量 [英] Checking microphone volume in Javascript
问题描述
我正在尝试制作一个需要访问用户麦克风的小游戏.我需要能够检查麦克风是否已连接,然后,如果连接了麦克风,请在游戏过程中检查通过麦克风的声音音量.我该怎么办?
I'm trying to make a little game that needs access to the users mic. I need to be able to check if a mic is connected, and then if so, check the volume of the sound coming through the mic through the duration of the game. How would I do this?
推荐答案
Here is the snippet needed to detect audio controls are available (pulled from: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia)
navigator.getUserMedia(constraints, successCallback, errorCallback);
以下是使用getUserMedia函数的示例,该示例使您可以访问麦克风.
Here is a sample using the getUserMedia function that will enable you access to the microphone.
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ audio: true, video: { width: 1280, height: 720 } },
function(stream) {
console.log("Accessed the Microphone");
},
function(err) {
console.log("The following error occured: " + err.name);
}
);
} else {
console.log("getUserMedia not supported");
}
这里是一个展示您想要的输入量"的存储库.
Here is a repository that demonstrates the "input volume" you desire.
https://github.com/cwilso/volume-meter/
这篇关于检查Javascript中的麦克风音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!