gpt4 book ai didi

javascript - WebRTC MediaStream 的麦克风事件级别

转载 作者:可可西里 更新时间:2023-11-01 01:22:34 27 4
gpt4 key购买 nike

我想要一些关于如何在 Chrome/Canary 中最好地获取音频 MediaStreamTrack javascript 对象的麦克风事件级别的建议。 MediaStreamTrack 对象是 getUserMedia 返回的 MediaStream 的音轨,作为 WebRTC javascript API 的一部分。

最佳答案

当麦克风有音频时,上下绿色条非常漂亮:

enter image description here

<script type="text/javascript">
navigator.webkitGetUserMedia({audio:true, video:true}, function(stream){
// audioContext = new webkitAudioContext(); deprecated OLD!!
audioContext = new AudioContext(); // NEW!!
analyser = audioContext.createAnalyser();
microphone = audioContext.createMediaStreamSource(stream);
javascriptNode = audioContext.createJavaScriptNode(2048, 1, 1);

analyser.smoothingTimeConstant = 0.3;
analyser.fftSize = 1024;

microphone.connect(analyser);
analyser.connect(javascriptNode);
javascriptNode.connect(audioContext.destination);

//canvasContext = $("#canvas")[0].getContext("2d");
canvasContext = document.getElementById("test");
canvasContext= canvasContext.getContext("2d");

javascriptNode.onaudioprocess = function() {
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var values = 0;

var length = array.length;
for (var i = 0; i < length; i++) {
values += array[i];
}

var average = values / length;
canvasContext.clearRect(0, 0, 60, 130);
canvasContext.fillStyle = '#00ff00';
canvasContext.fillRect(0,130-average,25,130);
}

}
);
</script>
<canvas id="test" style="background-color: black;"></canvas>

关于javascript - WebRTC MediaStream 的麦克风事件级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16724414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com